问题标签 [dynamic-dispatch]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
c# - Multiple Dispatch with Generics
I'm trying to abstract away my interface implementations by providing a factory/builder using generics. However, I'm running into an issue with multiple dispatch and C# generics at run time that's doing something that seems odd.
The basic scenario is I've defined several interfaces:
Then I have a factory class to return the actual implementations:
The issue is when the factory is called like this: new Factory().BuildModel<IAddressModel>()
The BuildModel(...) method that is dispatched at run time from generics is always the least derived form of T, in this case always object.
However, if you call new Factory().BuildModel(default(IAddressModel));
the correct method is displatched (most likely because this is done at compile time). It seems that dynamic dispatch with generics doesn't check the methods for the most derived type even though the method called should be the same whether it's done at compile time or run time. Ideally I'd like to make the BuildModel(...) methods private and only expose the generic method. Is there another way to get dynamic displatch to call the correct method at run time? I've tried changing the BuildModel<>()
implementation to return BuildModel((dynamic)default(T))
but this throws a run time error about not being able to determine which method to dispatch. Is there maybe a way to do this with contravariance and more interfaces?
java - 使用虚拟方法表在 C 中动态调度
我希望找到在 C 中实现动态调度的提示(最好是通过很好的例子)。
我正在学习 C,作为实践,我想使用动态调度虚拟方法表将 Java 转换为 C。
例如我有一个java代码:
我可以翻译一些基本的东西,比如:
我在尝试完成此操作时遇到困难,因为我不知道:
- 如何在 c 中定义这些方法。
- 设置这些方法的地址,
vtable
以便编译器识别要调用的正确方法。 - 还有什么要定义以使其工作。
ruby-on-rails - 在动态方法中更改实例变量(使用 ActiveResource 的动态资源)
我正在使用动态调度在从 ActiveResource 继承的类中定义几个类方法。
这适用于第一次通话,无论它是什么(Invoice.get_details、Invoice.get_pending_notifications...),但在第二次通话时总是失败。
我想了解为什么会发生这种情况以及我可以做些什么来解决它。
java - 如何在呼叫站点获取可能目标的数量?
在 Java 中,大多数方法都是虚拟方法。在调用点,可以根据对象调用不同的实际方法。这是动态调度。
问题:我们可以静态获取呼叫站点的目标数量吗?例如,来自字节码。
仅供参考:“虚拟方法”的答案有一个动态调度的简单示例。
java - 当“Fish f = new BlueFish();”时内部会发生什么?
假设我们有:
问题:
从程序集中,创建了一个 BlueFish 对象。但是为什么
javac
没有意识到BlueFish.speak()
应该调用呢?在运行时,JVM 如何处理
invokespecial #4
,以便调用正确的目标?
仅供参考,javap -c
输出是:
oop - 为什么称为“开放(或封闭)递归?
我找到了一些关于打开/关闭递归的解释,但我不明白为什么定义包含“递归”这个词,或者它如何与动态/静态调度进行比较。在我找到的解释中,有:
开放递归。大多数带有对象和类的语言提供的另一个方便的特性是,一个方法体能够通过一个特殊的变量调用同一对象的另一个方法,
self
或者,在某些语言中,this
. self 的特殊行为是它是后期绑定的,允许在一个类中定义的方法调用稍后在第一个子类中定义的另一个方法。[拉尔夫欣策]
...或在维基百科中:
的调度语义
this
,即对 this 的方法调用是动态调度的,称为开放递归,意味着这些方法可以被派生类或对象覆盖。相比之下,函数的直接命名递归或匿名递归使用封闭递归,具有早期绑定。
我还阅读了 StackOverflow 问题:什么是开放递归?
但我不明白为什么要用“递归”这个词来定义。当然,如果一个人通过执行“开放递归”来使用......方法递归调用,它可能会导致有趣(或危险)的副作用。但是这些定义没有直接考虑方法/函数递归调用(在维基百科定义中加上“封闭递归”,但这听起来很奇怪,因为“开放递归”并不指递归调用)。
你知道为什么定义中有“递归”这个词吗?是因为它基于另一个我不知道的计算机科学定义吗?仅仅说“动态调度”还不够吗?
c++ - Why does the cost of C++ virtual call depend on the number of derived classes?
EDIT: At the request of n.m., I have included the complete code I was using despite its verbosity.
I've written a short example program that I was using to study the overhead of virtual calls.
timer.h:
virtual.h:
virtual.cpp:
virtual_impl.cpp:
I compile virtual_impl.cpp into a shared library to prevent the compiler from messing around and devirtualizing or inlining things:
The result I get is:
As you can see, with 10 derived classes, the cost is nearly 3x greater than with only one derived class. Branch prediction seems like a likely target, but somehow on a list sorted by type, the performance is even worse than the random list!
EDIT2:
There doesn't seem to be any black magic occurring in the code generation. I found the disassembly for measure_call
here:
c++ - C++ 避免在派生类中使用 using 关键字进行动态分派
在下面的例子中,我会使用动态调度来调用虚函数吗?
我正在实现的类包含更多信息,但有一些我想为某些派生类保留默认值,但在其他类中覆盖,上面代码中的 fn() 就是一个例子。基本上,我只是使用基类来避免每次没有被覆盖时重写 fn() 。这是正确的方法吗?
谢谢!
java - 抽象类和子类,以及方法调用
大家好。
我对 servlet 中的抽象超类和子类有些困惑:
我有抽象的servlet超类:
和抽象超类,子类:
如果我调用子类,KidsBooksPage
我知道它init()
首先执行初始化方法,但问题是:
是什么让我的子类KidsBooksPage
调用抽象超类CatalogPage
。这个怎么运作??能明白。请给我解释一下。
谢谢你。
c++ - 在循环内使用不变多态类型进行优化
我有一个昂贵的循环,由于循环内for
的动态转换开销,它花费的时间比它应该的要多。
示例代码如下(可编译)
由于类在函数内部没有改变,我认为必须有一种方法可以避免循环内部的多态开销(和分支),并执行单个动态转换和单个函数调用,而不必多次编写循环.
我认为:
- 在
proceed
通话中投射。 - 访客模式。
我认为他们中的任何一个都不能解决问题。这些只是做同一件事的不同方式。
我正在考虑重新考虑我的设计,但在此之前,我很乐意听到您可能需要改进这段代码的任何想法和建议。