-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Expose isSubclassOf #58261
Comments
Similar to dart-lang/language#3837 and my answer on StackOverflow:
extension NullableObjectsExtensions<T> on T {
bool isSubtypeOf<S>() => <T>[] is List<S>;
bool isSupertypeOf<S>() => <S>[] is List<T>;
} So this way you can test any variable anywhere. |
They are similar but your request would require much more upwork to be delivered so I went with just a single method as Munificent expressed the dart does not have much request of those features. Those previous codes works only on simplistic class extensions but got mixed up pretty quickly in real cases. That's why I ended up with my own code to manage the class types :/ Munificent also shared that the infos is present in the VM but it's mainly a question of priorities. |
It works for types, but not really for values. It does work for all types. If you need to check two types for whether one is a subtype of the other (and presumably at least one is a type variable, otherwise the answer is constant), then doing: bool isSubtype<T1, T2>() => <T1>[] is List<T2>; will work for any two types. The only thing one can regret is that it requires allocating an object. You can use a simple class than class _SubtypeHelper<T> {}
bool isSubtype<T1, T2>() => _SubtypeHelper<T1>() is _SubtypeHelper<T2>; If you have two objects, then asking whether the runtime type of one is a subtype of the runtime type of the other is not possible. The referenced Java and Kotlin operatons work on class Type {
external bool operator <=(Type other);
external bool operator >=(Type other);
} which would allow something like That does mean that a type object needs to retain enough information to do subtype checking, which is something AoT compilers can otherwise tree-shake for any type that isn't statically detectable as occurring in a subtype check. Any use of It's probably clear that I'm fairly strongly against adding reflection-like operations. Java can get away with it, and having full reflection, because it's JIT compiled. It has the source class files available, so anything it hasn't compiled into the JIT'ed program already is still there. I'd sugges using |
Thank you for the very instructive information. I'm working on a large project with deep and wide implementations and used an Enum inside parent classes for implementation. It's verbose but work as intended as it's a simple field comparaison. It has large unit testing covering. When replacing this implementation with the specified method they fails with case everything going positive... Should this method able to handle mixins too and rich compositions? I'm gonna try to find the culprit but for sure they're not behaving the same, not what I would expecting from isSubTypeOf. Enum implementation does like a field inside the Vm would simply, I assume, keep a marker of the parent class. The use keep is very standard OOP. Replace a default set of implementations with another compatible based on values. But in a collection of subclasses I need to compare with which one is the parent for a transparent swap. You can't ask if it has a type that you don't know about. Using objects as types leaks their runtime type. Not quite reflection, but too close for comfort. Yet with my respect to the dart team priorities I still feel I'm gonna deeeeply miss this feature ;) Thanks everyone for helping me out. |
Summary: User requests |
Since the new addition of sealed, base and other OOP keywords,
dart offers more ways to model real world systems.
When using inheritance it is common pattern in chain of command as an exemple
to ask if a class is a subclass of specific parent to switch implementation.
Kotlin expose
isSubclassOf
, javaClass.isAssignableFrom
, etcI'm now facing a case where I need to handle a structure in parallel with the
classes declarations to handle hierarchy and it's getting large and error
prone to manually manage each new entry per class.
On Flutter Forum user tenhobie suggested this:
that works on simpler cases only.
When moving away from flutter and trying to model business relying on OP
this would be a considerable addition for large code base simplification.
Thank you for your feedbacks.
The text was updated successfully, but these errors were encountered: