[Proposal] Structural Typing with dynamic keyword. #969
Replies: 5 comments
-
How would it work with multiple interfaces? dynamic<Iface1, Iface2, Iface 3> foo;
//or
dynamic<Iface1 : Iface2 : Iface 3> bar; |
Beta Was this translation helpful? Give feedback.
-
With intersection types, it would be dynamic<Iface1 & Iface2 & Iface3> foo; |
Beta Was this translation helpful? Give feedback.
-
For the most part this proposal seems to be covered by shapes, except for the run time coercion. I don't really see a good way of implementing that doesn't involve reflection. Without the actual interface implementation you lose virtual dispatch and don't really have an alternative. I don't see the CLR providing that kind of functionality, although perhaps the shapes proposal can offer a kind of "unboxing" where the type can be negotiated and a duck-typing wrapper can be emitted at runtime. That seems like a lot of voodoo to put on the compiler, though. |
Beta Was this translation helpful? Give feedback.
-
I would rather having keyword defined like interfaces or classes can not implement or inherit from aspect, since it doesn't really make sense. aspects could expand each other by inheriting from each other.
No as far as classes or interfaces meets requirements to be duck typed, they can be assigned to aspect.
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Problem
C# provides
dynamic
keyword that allows us to write duck-typing code. However, this style of duck-typing is considered low-performance and hard to debug. A better way is structural typing. In this way, we specify an interface as the structural of the dynamic object. When converting a normal object into the structural dynamic type, the compiler or runtime would check whether it CAN (may not really DO) implement this interface, and results in compile error or runtime exception if it can't. In this way, we can approach duck-typing without sacrificing compile-time type checking or intellisense, and we can do more things to optimize.Proposal
We can use
dynamic<IA>
to represent a dynamic object with structural interface IA.Now let's think about optimization. First, in
Test(new A1());
, because class A1 implements IA, it doesn't need dynamic binding at all, we can directly cast it to IA. Though inTest(new A2());
we do need dynamic binding as class A2 doesn't implements IA, we can do some optimizations base on the fact that method A must exist because of type checking.Beta Was this translation helpful? Give feedback.
All reactions