Enable deconstructing dynamic objects #2071
Replies: 11 comments
-
I think we should first get some experience with the semantics of non-dynamic code in this case, and some compelling use case. It's a complex feature to implement this for dynamic... |
Beta Was this translation helpful? Give feedback.
-
@markusschaber , do you see any non-trivial edge cases? Anything, that existing binder can not handle? |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
@alrz I am looking for cases where semantics would be unclear. I understand reusing current implementation is not straightforward. However, I can see that feature being very useful for Python interop, where if one would want to reuse Python code, that returns a tuple, it should be easy to deconstruct it without figuring out types of components in advance. var (train, test) = load_dataset();
// or dynamic (train, test) = ...
// or int (train, test) = ... if you know the actual tuple element types
model.fit(train) |
Beta Was this translation helpful? Give feedback.
-
@alrz Could that just be |
Beta Was this translation helpful? Give feedback.
-
@jnm2, there's no issue on how to define the deconstruction in the class. That part is untouched. It's only about choosing the right overload at the receiving side based on whether the resulting types are specified or not, and what are they. And then the actual implementation. |
Beta Was this translation helpful? Give feedback.
-
@lostmsu Right; I think that's what @alrz and I are talking about. There is no such thing as |
Beta Was this translation helpful? Give feedback.
-
@jnm2 Yes, that's exactly what you get with simple local declarations as well. @lostmsu I think Joseph is talking about the out argument at the call site that the compiler should generate, not the method declaration. |
Beta Was this translation helpful? Give feedback.
-
I'm not familiar with Python interop, but the resulting dynamic object must provide a |
Beta Was this translation helpful? Give feedback.
-
@alrz, it would be up to the interop layer (e.g. Python.NET). They could simply return a ValueTuple or override TryInvokeMember |
Beta Was this translation helpful? Give feedback.
-
Isn't this extremely straightforward? Just transform: dynamic obj = Whatever;
(Type1 a, Type2 b) = obj; to: dynamic obj = Whatever;
obj.Deconstruct(out Type1 a, out Type2 b); ? I suppose two complications arise:
dynamic obj = Whatever;
if (obj.GetType().Name.Contains("ValueTuple")) // I can't be bothered looking up the correct way to do this right now
...
else
{
obj.Deconstruct(out Type1 a, out Type2 b);
} b) Just add a Deconstruct method to ValueTuple. You will need to add for every single possible length, so deconstructing a dynamic tuple greater than a certain length wont work. No great loss. You could have a roslyn analyser warn if you try and do that. |
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.
-
I imagine it is possible to find matching
Deconstruct
in the object at runtime either based on target types, or, if target types are unknown as invar (a,b) = default(dynamic)
, set both to have typedynamic
.Beta Was this translation helpful? Give feedback.
All reactions