我如何准备好在我的窗户上发送活动?例如,在我的代码中,我得到了:
$("#info").click(function(){
// do stuff
});
Run Code Online (Sandbox Code Playgroud)
我需要在没有点击#info的情况下第一次调用这个函数,就像//做东西一样.
首先,我了解如何使用函数指针和字符串或其他查找来实现调度表,这不是挑战.
我正在寻找的是在编译时动态地向该表添加条目的一些方法.
我希望的代码结构类型是这样的:
Strategy.h - 包含调度程序和调度表定义的函数定义Strategy.c - 包含调度程序的代码
MyFirstStrategy.c - 包括Strategy.h并提供策略MyOtherStrategy.c的一个实现 - 包括Strategy.h并提供策略的第二个实现
我们的想法是,将函数指针和策略名称插入到调度表中的代码不应该存在于Strategy.c中,而应该存在于各个策略实现文件中,并且查找表应该以某种方式在编译时动态构造.
对于固定大小的调度表,这可以如下管理,但我想要一个动态大小的表,我不希望Strategy.c实现必须包含实现的所有头文件,我想发送要在编译时构造的表,而不是运行时.
固定大小示例
typedef void strategy_fn_t(int);
typedef struct {
char *strategyName;
strategy_fn_t *implementation;
} dispatchTableEntry_t;
Run Code Online (Sandbox Code Playgroud)
#include "Strategy.h"
void firstStrategy( int param );
Run Code Online (Sandbox Code Playgroud)
#include "Strategy.h"
void otherStrategy( int param );
Run Code Online (Sandbox Code Playgroud)
#include "Strategy.h"
#include "MyFirstStrategy.h"
#include "MyOtherStrategy.h"
dispatchTableEntry_t dispatchTable[] = {
{ "First Strategy", firstStrategy },
{ "Other Strategy", otherStrategy }
};
int numStrategies = sizeof( dispatchTable ) / sizeof(dispatchTable[0] );
Run Code Online (Sandbox Code Playgroud)
我真正想要的是一些预处理器魔法,我可以将其插入到策略实现文件中以自动处理这个问题,例如
我是新来的反应/ redux.我试图弄清楚redux中的所有部分是如何相互作用的.给我带来麻烦的一件事是理解行动和减速器之间的关系.调用操作时,商店如何知道要使用哪个减速器?它是否完全基于动作类型名称?类型名称必须是唯一的吗?reducer将新状态对象传递给的是什么或者什么,商店还是动作?
据我了解,它是这样的:
我对Java Overload Methods有疑问.
假设我有一个重载方法foo:
public static String foo(String x) {
return "foo-String: " + x;
}
public static String foo(Object x) {
return "foo-Object: " + x;
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能实现像这样的功能
public static String useString() {
return(foo("useString"));
}
public static String useObject() {
return(foo("useObject"));
}
Run Code Online (Sandbox Code Playgroud)
其中一个使用重载的字符串方法,一个是重载的对象方法?
foo-Method的调用应该使用String输入.(这意味着我不想和演员一起工作
return(foo((Object)"useObject"));
Run Code Online (Sandbox Code Playgroud)
也许你可以帮我解决这个问题
上面,只是练习的一个例子.我试图更好地理解Overloads和Dispatch,并且正在寻找用于调用(和选择)重载方法的替代解决方案.
我已经为此工作了很长时间,但没有找到解决方案:
Cannot read property 'dispatch' of undefined每当我得到handleInputChange调用函数。
这是我的应用程序的基本布局:
App.tsx
import * as React from 'react';
import { BrowserRouter as Router, Route, Switch} from 'react-router-dom';
import SuperSignupScreen from './screens/SuperSignupScreen'
import './App.css';
import {connect, Dispatch} from "react-redux";
export type SessionProps = {
}
class AppImpl extends React.PureComponent<SessionProps & { dispatch: Dispatch<{}> }, {}> {
render() {
return(
<Router>
<Switch>
<Route path="/superSignup" component={SuperSignupScreen}/>
<Route key="404" component={NotFoundScreen} />
</Switch>
</Router>
)
}
}
function mapStateToProps(state: { session: SessionProps }): SessionProps { …Run Code Online (Sandbox Code Playgroud) 我正在尝试解决这个问题,这实际上是由另一个与callwithand 的不同行为相关的stackoverflow问题引发的samewith.后者似乎有明确的定义,然而,它并不是那么清楚callwith.
看看这个例子:
proto how-many(|) {*}
multi sub how-many( Pair $a, Pair $b ) {
say "Int $a and $b";
return "There are $a and $b"
}
multi sub how-many( $a, $b ) {
say "Not int $a and $b";
my $calling = callwith( 1 => $a, 2 => $b );
return $calling;
}
say how-many( "little piggie","littler piggie" );
Run Code Online (Sandbox Code Playgroud)
根据文档,callwith应该调用下一个匹配的候选人.但是,这是输出:
Not int little piggie and …Run Code Online (Sandbox Code Playgroud) 我想对 Swift 中的方法调度有一个深入的了解。我从这个流行的博客中读到了以下三种类型的调度:
在那篇博客中,作者说 NSObject 子类型维护一个调度表(见证表)和一个消息调度层次结构。作者分享的代码片段如下:
class Person: NSObject {
func sayHi() {
print("Hello")
}
}
func greetings(person: Person) {
person.sayHi()
}
greetings(person: Person()) // prints 'Hello'
class MisunderstoodPerson: Person {}
extension MisunderstoodPerson {
override func sayHi() {
print("No one gets me.")
}
}
greetings(person: MisunderstoodPerson()) // prints 'Hello'
Run Code Online (Sandbox Code Playgroud)
我将引用作者对 Person 实例调用 sayHi()的推理:
greetings(person:) 方法使用表调度来调用 sayHi()。这按预期解决,并打印“Hello”。这里没有什么太令人兴奋的了。现在,让我们继承 Person
作者继续解释了在 MisunderstoodPerson 类型转换为 Person 的实例上调用sayHi():
请注意, sayHi() 是在扩展中声明的,这意味着该方法将通过消息调度来调用。当调用 greetings(person:) …
我目前正在尝试实现一个集成在我的 angular 应用程序中的面板,这将允许我使用与 redux-devtools 相同的导入功能导入从 redux-devtools 导出的任何 json 状态文件。
我的应用程序与@ngrx/store-devtools 正确集成。
我不知道如何从我的组件中检索 devtools 存储,然后像我在 redux-devtools 代码中看到的那样调度操作 IMPORT_STATE:
store.liftedStore.dispatch({type: 'IMPORT_STATE', ...nextLiftedState});
Run Code Online (Sandbox Code Playgroud)
目标是从 redux-devtools 手动触发导入状态功能,但直接在我的应用程序的组件内。
有可能这样做吗?以及如何在我的组件中注入这个存储然后使用它?
提前致谢,
编辑:
实际上,我想要实现的是在我的应用程序中有一个组件,它允许我导入不同的状态(作为 json 文件),我以前从 redux-devtools 扩展中记录到我的应用程序的任何页面。因此,该组件需要访问 redux-devtools 存储并分派操作 IMPORT_STATE。我目前所做的似乎没有触发 redux-devtools 商店的 IMPORT_STATE 操作的减速器。我想我错过了一些东西来包含来自 angular 应用程序的 redux-devtools 存储。
你知道如何实现这一目标吗?
提前致谢,
作为关于在单个程序中使用不同 API 的这个问题的后续,Liz Mattijsen 建议使用常量。现在这里有一个不同的用例:让我们尝试创建一个multi按 API 版本区分的,如下所示:
class WithApi:ver<0.0.1>:auth<github:JJ>:api<1> {}
my constant two = my class WithApi:ver<0.0.1>:auth<github:JJ>:api<2> {}
multi sub get-api( WithApi $foo where .^api() == 1 ) {
return "That's version 1";
}
multi sub get-api( WithApi $foo where .^api() == 2 ) {
return "That's version deuce";
}
say get-api(WithApi.new);
say two.new.^api;
say get-api(two.new);
Run Code Online (Sandbox Code Playgroud)
我们对第二个版本使用常量,因为两者不能在一个符号空间中一起使用。但这会产生此错误:
That's version 1
2
Cannot resolve caller get-api(WithApi.new); none of these signatures match:
(WithApi $foo where { …Run Code Online (Sandbox Code Playgroud) 在这个项目中,我使用React.createContext()方法来管理状态。我可以通过导入Context.Consumer并在类组件(或功能组件)的返回方法中使用此使用者来访问状态数据。请参阅下面的示例:
function App(){
return(
<Consumer>
{value=>{
const {basket} = value;
return <p> { basket.length() } </p>
}
}
</Consumer>
)
}Run Code Online (Sandbox Code Playgroud)
问题是我想从useEffect 挂钩分派一个操作,而不是从组件返回
这就是我想做的:
function App() {
useEffect(() => {
auth.onAuthStateChanged(authUser => {
if(authUser){
dispatch({ //I need to use dispatch methode here!!
type : 'SET_USER',
payload : authUser
})
}
else{
dispatch({ //I need to use dispatch methode here!!
type : 'SET_USER',
payload : null
})
}
})
}, []) …Run Code Online (Sandbox Code Playgroud)