I want to make use of the static type benefits of dart and limit incoming Type of a function to specific classes.
As time of writing I have not thought of a design to achieve that (if it is even possible).
Wish:
abstract class UpperClass {}
class Class1 extends UpperClass {}
class Class2 extends UpperClass {}
class Class3 {}
void justAcceptSpecific(Type type) => print("doesn't matter");
void main() {
justAcceptSpecific(Class2); // works
justAcceptSpecific(Class3); // linting should show wrong type
justAcceptSpecific(2); // error like here
}
I thought about using an abstract class, but the issue is, that I do not want to pass an object, but just the type.
This would work as intended (but it passes the object :/ ):
abstract class UpperClass {}
class Class1 extends UpperClass {}
class Class2 extends UpperClass {}
class Class3 {}
void justAcceptSpecific(UpperClass type) => print("doesn't matter");
void main() {
justAcceptSpecific(Class2());
justAcceptSpecific(Class3()); // linting shows wrong type, which is like it should be
}
If that is of relevance, I intend to use it in a constructor and just made my examples here as function calls for simplicity sake.
question from:
https://stackoverflow.com/questions/65557471/limited-type-as-a-passing-parameter-in-dart-linting 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…