Downcast subtype operator #7991
-
The cast operator using () or “as” allows writing any target type. Before: MyType m:
(String)m // compiles , always fail in runtime
(MySubType) // compiles workaround: class MyTypeExt{
public static T Cast<T>(this MyType m) where T:MyType => (T)m;
}
// repeat for any type needed With subtype operator: (syntax just an example) m subtype_cast string // doesn’t compile
m subtype_cast MySubType // compiles |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
By default both |
Beta Was this translation helpful? Give feedback.
-
Are you sure? If I try to cast from |
Beta Was this translation helpful? Give feedback.
By default both
x as X
and(X)x
gives you a warning if it's not a hierarchy. Make those warnings into errors and you're good - no changes required.