-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should extension structs be allowed to define methods also listed in their interfaces? #2370
Comments
This continues to feel like a foot gun to me if we allow it. There is almost no reason that I can think of for implementing |
Definite footgun. Take an object implementing I worry that even giving extension structs a class-like syntax is a mistake, and the "view" proposal is better because it doesn't look like a class. |
Yeah we talked about this for interop mocking, and I agree. If we disallow this, is there any use to having extension structs implement anything? |
I think two models are promising:
With model 1, overriding is an error, and this means that there is no doubt that invocation of an implemented method will call the method on the underlying representation object. With model 2, overriding is allowed, and the declaration in the extension struct is faithfully preserved as the implementation which is actually running: Either we use static resolution on an unboxed representation, or we use normal OO dispatch on a boxed representation. |
yes, very much. In my proposal, if the underlying object implements something, you can expose that, and consumers can use it. So for example, you can have a |
I'm fairly reluctant to having something that says
In general, this part is something that I still feel is under-motivated. If we do something along the lines of the view proposal, my starting point would be that we not provide this functionality. |
If we leave out everything about Alternatively, if we do support boxing then I would expect that we are able to specify and enforce that the boxed object has certain subtype relations, because otherwise the boxed objects wouldn't be very useful. And I do think that it's an inconvenient and substantial reduction of the expressive power of the mechanism, if the boxed object can only have a type which is essentially the same as the type of the underlying representation object. As a third alternative, consider an extension struct / view If we support |
I think this is more-or-less settled as "yes", with redeclarations. |
In the extension struct proposal (#2360) I allow extension structs to implement class interfaces, in the case that their representation type implements the interface. This allows extension structs to expose the fact that the underlying representation implements an interface. Extension structs are forbidden from providing their own implementation for methods exposed via interfaces in this manner, to avoid the surprising behavior that such methods are statically dispatched, and hence any dispatch of interface methods through the interface will continue to reach the original implementation from the underlying object. Example:
This is surprising, and it seems to me to be even more surprising than in the extension case (#2369), since rather than extending a struct, we are here claiming to implement a class interface. The primary use of interfaces is to support writing code which is polymorphic over interfaces, allowing declarations to provide their own implementations. With extension structs however, the provided implementations are never used when accessed via that interface. In contrast, extension provides both polymorphism and inheritance, and extension structs are never polymorphic.
The text was updated successfully, but these errors were encountered: