[Proposal]: Extension interface implementation #9320
Replies: 2 comments 7 replies
-
Responding to one of the comments from @HaloFour
Rust's rules are called orphan rules and they are very similar to the rules that I listed in the linked discussion. However, the motivation is not the same, and it turns out this is closer to "coincident evolution" than a mirroring of ideas. Rust has orphan rules primarily because they want to guarantee particular notions of consistency in their type system and avoid some challenges with typing in a system where multiple implementations can "cross" each other. In contrast, the orphan rule I propose here is mainly about implementation choices. Since the very beginning of .NET there have been two things that have been built heavily into the runtime: interface implementation is part of type identity, and type identity is fixed. Changing either of these things is almost impossible in .NET. You start messing with type identity and things blow up. You start trying to separate interface implementation from type identity, things blow up. What the orphan rule brings to .NET is the ability to adjust the interface implementation list without changing type identity. Because everything is coherent and entirely in the view of the runtime at type load time, the runtime can effectively shim any set of interface implementations into existing types without changing identity. That's not to say this is an easy task, but it's at least understandable. This would still be an incredibly tricky change to the runtime, but I think it's possible. Absent an orphan rule, I'm skeptical that any alternative is going to progress beyond brainstorming. For anyone who is interested in tackling that challenge: you need to start at the ECMA-335 level. Your task will be to walk every single rule regarding type identity, conversions, interface implementation, interface dispatch, generics, and reflection, and describe precisely how they will behave in your design. At minimum, I expect you will need to answer the following problem: |
Beta Was this translation helpful? Give feedback.
-
I like this proposal. Massively subjective of course: Personally, I don't seem to recall any case where I would've needed or liked to implement some third party interface for some third party type. If I ever had to, I'd imagine that effectively wrapping objects into some decorating type, either statically or via dynamic proxy and mixing in some new interface implementation would typically get the job done? Similarly, I'm not a huge fan of the idea of scope-bound extension interface implementation just yet. Makes code harder to follow (granted, same can be said about extension methods). And too many question marks for my liking.. say you have a function taking a parameter of type object. Would a "obj is ISomeExtensionInterface" check succeed if the caller of said function had the extension in scope? If the extension is in scope for the function itself? What if the type check was reflection based? Anyways, back to this proposal here: Conditional interface implementation would be quite nice to have. Especially for functional code - think of option or result types that could implement iequatable, icomparable etc if and only if their inner type does as well. For understanding, could the orphan rule be relaxed further? For example, could you allow an extension for an external interface of some external type if it's defined in the application's entry assembly - I'd imagine maybe that might be scannable before implementing type or interface is loaded? |
Beta Was this translation helpful? Give feedback.
-
Discussion for proposal #9319
Beta Was this translation helpful? Give feedback.
All reactions