Implicit interfaces using source generators?
I see new source generators for C# 5, and I wonder if it will be possible to generate interface for every type (just like dart does). I think it would be awesome feature!
7 comments · 75% upvoted · other discussions
The mirror feature would be much more interesting. What if the generator checks every assignment you make with an object of a class to an interface and then if the class has the necessary members it adds the interface to the list of implemented interfaces. That would be some form of structural typing.
SleepCodeRepeat [OP] 1 points · May 2020
This is impossible in c#, because assignments may happen in clients of the code, not just in code. To do such feature you would need to evaluate this at runtime, which is too much overhead I think.
Basically that's what they do with tuple deconstruction, or await - but you need compiler support for this.
Eirenarch 1 points · May 2020
It is not possible to handle the general case but you can handle cases in your code which can be beneficial for a great amount of cases.