-
Notifications
You must be signed in to change notification settings - Fork 66
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
Add shadow classes as a way to extend sealed classes. #204
Comments
How would this be helpful? |
A lot of the classes in the .Net framework are sealed making it impossible to extend them. |
Can you provide an example of where this would be useful? |
@reduckted It's absolutely not daily routine, but sometimes I need a change in an existing framework class, and then I find that the class is sealed, so I am ending up in the complete re-implementation of the class instead of inheriting and make the change. With the cost of a few days of work instead of a few minutes, |
In the string case you provide, isn't a problem that no methods anywhere which take a String would be able to take your FixedLengthString? Since this would not be true inheritance, none of the polymorphism stuff would actually work. And if the type you're shadowing has a base type... that type hierarchy won't be preserved in the shadow. Is this not a problem? |
This is solved by the auto-generation of the widening and narrowing operators, so it will using option strict off. If option strict is on, you can use the .base property. The type hierarchy is preserved as much as possible because the shadowed class uses the same base class as the class that is shadowed. Example of the extra code needed in the generated class:
About polymorphic - it would not work, just like it will not work for shadowed methods in in normal inherited classes. |
deleted this comment |
It enables me to extend sealed classes that I otherwise have to build from the ground up
In any case that you need a specialized version of a sealed class Other possible questions: |
We have a way to re-implement non-overridable (sealed) methods, fields and properties using the Shadows keyword.
But on class-level we do not have a way to override non-inheritable (sealed) classes.
I'd like to see the Shadow keyword for inheritance too, making the following syntax valid:
Public Class MyClass
Inherits Shadows System.String
End Class
The inherited class should 100% act like system.string, but with the possibility to reimplement (shadow) all public members.
An example of the internal representation of the shadowed class can be seen with the extension "ShadowClasses" (link: https://marketplace.visualstudio.com/items?itemName=theoverweij.ShadowClasses-Extension-For-VB )
The text was updated successfully, but these errors were encountered: