3 ms·
Thanks. This does look very nice. How does the runtime know if you can “Deconstruct” an object? Is there an interface that every Object inherits? Or is it impli
by drewmate 9y ago
Thanks. This does look very nice. How does the runtime know if you can “Deconstruct” an object? Is there an interface that every Object inherits? Or is it implicit based on whether the object implements the appropriate method?
If the latter, C# is gaining some very golang-like attributes. I also noticed the special ‘Range’ keyword and Discarded assignments with underscore. I think this can only be a good thing. C# and go are among my favorite languages to use and the more good ideas they adopt from the other, the better.
- Analemma_ 9y agoI assume it is the latter: since the creation of the tuple is happening at compile-time, it's easy and effectively free to just quickly check with reflection if there is a Deconstruct() method.
- SideburnsOfDoom 9y ago> How does the runtime know if you can “Deconstruct” an object? It implicit based on whether the object implements the appropriate method? Based on what I have read below, it's implicit; there's very little that the runtime has to know - it happens at compile time, with the compiler looking for a method with the name "Deconstruct" and the right type signature, and plugging that in during the compile. The compiler already does a lot of heavy lifting codegen for language features like async / await https://andrewlock.net/deconstructors-for-non-tuple-types-in-c-7-0/ https://andrewlock.net/deconstructors-for-non-tuple-types-in... http://www.techcartnow.com/new-c-sharp-7-features-in-action-deconstruction-in-c-sharp-7/ http://www.techcartnow.com/new-c-sharp-7-features-in-action-...
- pauldino 9y agoIt's implicit based on the appropriate method overload being present. In fact that's the way foreach loops and LINQ already work in C#, you don't actually need to implement IEnumerable or IQueryable to use them. https://blogs.msdn.microsoft.com/ericlippert/2011/06/30/following-the-pattern/ https://blogs.msdn.microsoft.com/ericlippert/2011/06/30/foll...
- drewmate 9y agoAh, that’s interesting. I always just assumed everything I was using inherited IEnumerable somewhere way down the line. Thought it’s possible that was the case too. It’s been a few years for me and I didn’t do much outside of standard library / entity framework.