dynamic-dispatch

Does GHC use dynamic dispatch with existential types?

让人想犯罪 __ 提交于 2021-01-27 09:54:58
问题 Does the following bit of code use dynamic dispatch as it's understood in C++ or Java? As I understand, at the last line, compiler cannot possibly know at compile time which implementation of (==) to call, yet the code compiles and produces correct results. Can someone please explain, what kind of implementation (vptr, for example) lies behind this? {-# LANGUAGE ExistentialQuantification #-} data Value = A Int data ForallFunc = forall a. Eq a => Forall (Value -> a) unpackA (A int) = int

How to use dynamic dispatch with a method which takes an iterator as a parameter?

荒凉一梦 提交于 2020-12-06 12:21:00
问题 I am writing a command line application in rust for processing audio from a sensor. I would like the user to be able to choose an algorithm or filter to apply from several options. I was hoping to use dynamic dispatch to switch out a struct which implements my filter trait at runtime. However, this is not allowed by the compiler, because one of the trait methods takes a generic parameter. How could I implement this same functionality, without causing any compiler troubles? I know that an easy

How to use dynamic dispatch with a method which takes an iterator as a parameter?

倖福魔咒の 提交于 2020-12-06 12:20:49
问题 I am writing a command line application in rust for processing audio from a sensor. I would like the user to be able to choose an algorithm or filter to apply from several options. I was hoping to use dynamic dispatch to switch out a struct which implements my filter trait at runtime. However, this is not allowed by the compiler, because one of the trait methods takes a generic parameter. How could I implement this same functionality, without causing any compiler troubles? I know that an easy

Multiple Dispatch with Generics

大城市里の小女人 提交于 2020-01-24 12:25:05
问题 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: public interface IAddressModel { } public interface IUserModel { } Then I have a factory class to return the actual implementations: public class Factory { public T BuildModel<T>() { return BuildModel(default(T));

Adopting CustomNSError in DecodingError

眉间皱痕 提交于 2019-12-12 09:52:57
问题 I'm writing an error logger using Crashlytics and I've come up against an issue that is making me question my understanding of protocols and dynamic dispatch. When recording non fatal errors using Crashlytics the API expects an Error conforming object, and an optional user info dictionary. I'm looking at JSON decoding errors at the moment, and I wasn't too happy with what I was seeing in the Crashlytics dashboard when I just sent the DecodingError along in recordError. So my solution was to

How to tell if a program uses dynamic dispatch by looking at the assembly

余生长醉 提交于 2019-12-10 17:14:06
问题 I read a post on Reddit on Herb Stutter: JIT will never be as fast as native and somebody made a comment that it was incredible somebody called Herb "misinformed" that C# uses virtual methods instead of non-virtual (you can read the article here). It got me to thinking and I made a quick little program and noticed that C# does in fact generate virtual methods for CIL (callvirt vs call). But then I was told it wasn't that easy and that the JIT may inline the code instead of using vtables and

Why can't the virtual function table pointer (vfptr) be static in C++?

半腔热情 提交于 2019-12-06 02:54:16
问题 If the virtual function table is the same for all objects of the class, then why can't the pointer to that table (vfptr) be static and be shared across all the objects? 回答1: The vtable is essentially static. But you need a vptr member actually inside the object to do virtual dispatch and other RTTI operations. On a vptr implementation, this C++ code: class Base { public: virtual void f(); }; class Derived : public Base { public: virtual void f(); }; may act similarly to something like this:

Work around Java's static method dispatching without Double Dispatch/Visitor patterns

末鹿安然 提交于 2019-12-06 01:26:04
I am using a class Foo that provides these methods: String overloadedMethod(Object) String overloadedMethod(Goo) Since Java statically dispatches on the non-receiver argument, I cannot just pass my value (which is an Object , but might have dynamic type Goo ) and rely on the JVM to dynamically choose the "correct" method. This is my current (ugly) work-around: Object value = ...; Foo foo = new Foo(); if (value instanceof Goo) { Goo gooValue = (Goo) value; return foo.overloadedMethod(gooValue); // -> overloadedMethod(Goo) } else { return foo.overloadedMethod(value); // -> overloadedMethod

Why is it called "open (or closed) recursion?

自闭症网瘾萝莉.ら 提交于 2019-12-04 16:12:23
问题 I found some explanations of open/closed recursion, but I do not understand why the definition contains the word "recursion", or how it compares with dynamic/static dispatching. Among the explanations I found, there are: Open recursion. Another handy feature offered by most languages with objects and classes is the ability for one method body to invoke another method of the same object via a special variable called self or, in some languages, this . The special behavior of self is that it is

Why can't the virtual function table pointer (vfptr) be static in C++?

跟風遠走 提交于 2019-12-04 08:11:00
If the virtual function table is the same for all objects of the class, then why can't the pointer to that table (vfptr) be static and be shared across all the objects? The vtable is essentially static. But you need a vptr member actually inside the object to do virtual dispatch and other RTTI operations. On a vptr implementation, this C++ code: class Base { public: virtual void f(); }; class Derived : public Base { public: virtual void f(); }; may act similarly to something like this: class Base { public: Base::Base() : m_vptr(&Base_vtab) {} Base::Base(const Base& b) : m_vptr(&Base_vtab) {}