我想知道Java7的新invokedynamic字节码指令是否可用于实现Java语言的多个调度.java.lang.invoke下的新API是否有助于执行此类操作?
我正在思考的场景如下所示.(这看起来像访问者设计模式的应用案例,但可能有理由认为这不是一个可行的选择.)
class A {}
class A1 extends A {}
class A2 extends A {}
class SomeHandler {
private void doHandle(A1 a1) { ... }
private void doHandle(A2 a2) { ... }
private void doHandle(A a) { ... }
public void handle(A a) {
MultipleDispatch.call(this, "doHandle", a);
}
}
Run Code Online (Sandbox Code Playgroud)
然后库类MultipleDispatch会做类似的事情:
class MultipleDispatch {
public static Object call(Object receiver, String method, Object...arg) {
// something like that in byte code
#invokeDynamic "doHandle" "someBootstrap"
}
static …Run Code Online (Sandbox Code Playgroud) 它是否与重载相同,如果没有,你可以在C#中提供每个例子吗?
我已经阅读了对SO中提出的类似问题的回复......我不理解发布给它的回复.
类似的问题在这里问
编辑:使用C#4.0中新的"动态"关键字...这会使语言"多调度"启用吗?
单个调度的问题对于使用Java和C#等静态类型语言进行编码的人来说大多是熟悉的.基本思路是:
虽然运行时多态性允许我们根据类型(运行时类型)调度到正确的方法调用receiver,例如:
IAnimal mything = new Cat();
mything.chop();
Run Code Online (Sandbox Code Playgroud)
方法调用将根据运行时类型执行mything,即Cat.这是单一调度功能(存在于Java/C#中).
现在,如果您不仅需要调度接收器的运行时类型,还需要调度(多个)参数的类型,那么您将面临一个小问题:
public class MyAcceptor {
public void accept (IVisitor vst) {...}
public void accept (EnhancedConcreteVisitor vst) {...}
}
Run Code Online (Sandbox Code Playgroud)
第二种方法永远不会被调用,因为在我们的"消费者"代码中,我们倾向于通过常见的超类型或接口来处理不同类型的对象(在我的示例中为访问者).
这就是我问的原因 - 因为动态类型允许多重调度多态,C#4.0有动态关键字;)
我读使用了现代C++设计泛型编程和设计模式由安德烈Alexandrescu的和章11多方法的交易正好与我试图解决这个问题.本书的所有源代码都发布在一个名为Loki的库中.
问题是,这本书是相当老的(2001)和有限制的那些交易不再是真实的C++ 11(如该模板参数的数量不能是变量).我试着查看Loki是否已经使用C++ 11进行了重写,但是最后一次修改是在2009年,并且Andrei Alexandrescu的主页上没有更新.然而,在做了一些研究后,我觉得Loki就像Boost一样,它被合并到标准库中.
C++ 11中是否采用了多方法成语或部分内容?
现在在本页(http://landoflisp.com)上阅读Lisp,我在页面上的第二个段落中找到了以下语句,单击链接CLOS GUILD时显示:
关于该示例的重要注意事项是,为了确定在给定情况下调用哪种混合方法,CLOS需要考虑传递给该方法的两个对象.它基于多个对象的类型调度到该方法的特定实现.这是传统的面向对象语言(如Java或C++)中不可用的功能.
这是示例Lisp代码:
(defclass color () ())
(defclass red (color) ())
(defclass blue (color) ())
(defclass yellow (color) ())
(defmethod mix ((c1 color) (c2 color))
"I don't know what color that makes")
(defmethod mix ((c1 blue) (c2 yellow))
"you made green!")
(defmethod mix ((c1 yellow) (c2 red))
"you made orange!")
Run Code Online (Sandbox Code Playgroud)
不,我认为最后一句话是错的.我实际上可以使用以下Java代码完成此操作:
public class Main {
public static void main(String[] args) {
mix(new Red(), new Blue());
mix(new Yellow(), new Red());
}
public …Run Code Online (Sandbox Code Playgroud) 我可以根据一些非参数值选择一个多,但我必须至少有一个参数,所以我可以where在那里克服:
our $*DEBUG = 1;
debug( 'This should print', 'Phrase 2' );
$*DEBUG = 0;
debug( 'This should not print' );
multi debug ( *@a where ? $*DEBUG ) { put @a }
multi debug ( *@a where ! $*DEBUG ) { True }
Run Code Online (Sandbox Code Playgroud)
我似乎记得有人曾经在multis中发送一些没有参数的技巧.例如,我有一个show-env例程,我想在周围散布,如果我设置了一些调试条件,它只会做任何事情.我可以像我所展示的那样实现它,但这并不是很令人满意,而且我想象的其他地方并不聪明:
our $*DEBUG = 1;
debug( 'This should print', 'Phrase 2' );
show-env();
$*DEBUG = 0;
debug( 'This should not print' );
show-env();
multi debug ( *@a where ? $*DEBUG ) { …Run Code Online (Sandbox Code Playgroud) 我有这些课程:
class Asset
{ }
class House:Asset
{ }
Run Code Online (Sandbox Code Playgroud)
考虑这些局外人的静态功能:
static void Foo (Asset a) { }
static void Foo (House h) { }
Run Code Online (Sandbox Code Playgroud)
如果我写:
House h = new House (...);
Foo(h);
Run Code Online (Sandbox Code Playgroud)
它会调用Foo(House)(编译时绑定)
如果我写:
Asset a = new House (...);
Foo(a);
Run Code Online (Sandbox Code Playgroud)
它会调用Foo(Asset) (编译时绑定)
目标:访问运行时类型方法:
我有2个选择:
1)使用动态如下:
Asset a = new House (...);
Foo ((dynamic)a); // NOW it will call Foo(House)
Run Code Online (Sandbox Code Playgroud)
2)将功能从使用移动static到override使用polymorphism mechanism.
问题:
有没有其他方法(没有将功能移动到polymorphism mechanism|| dynamic …
我想知道是否有一种方法可以使用符号进行多次调度,但也包括"全能方法".即类似的东西
function dispatchtest{alg<:Symbol}(T::Type{Val{alg}})
println("This is the generic dispatch. The algorithm is $alg")
end
function dispatchtest(T::Type{Val{:Euler}})
println("This is for the Euler algorithm!")
end
Run Code Online (Sandbox Code Playgroud)
第二个工作和匹配手册中的内容,我只是想知道如何让第一个工作.
I am using multipledispatch to make a Point class, which has three constructors: one that takes a single integer, one that takes two, and one that takes an object of type Point. But I am unable to implement the third constructor as I don't know what argument to give to the @dispatch decorator, since the class Point is not yet defined. I have currently resorted to using object, but is there any way I can use Point itself?
Here's …
这是程序:
my %SUB-COUNTS;
say "Start";
multi sub trait_mod:<is>(Sub $s where .name().contains("-logged"), :$AOP) {
$s.wrap({
say "Entering { $s.name }";
callsame;
});
}
multi sub trait_mod:<is>(Sub $s where .name().contains("-counted"), :$AOP) {
$s.wrap({
say "Counting { $s.name }";
%SUB-COUNTS{$s.name}++;
});
}
sub water-logged() is AOP {
return "Water";
}
sub out-logged() is AOP {
return "Out";
}
sub we're-counted() is AOP {
state $count = 0;
return $count++;
}
sub we're-counted-and-logged() is AOP {
state $alpha = 'a';
return $alpha++;
}
say …Run Code Online (Sandbox Code Playgroud)