Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
226 views
in Technique[技术] by (71.8m points)

abstraction - Limited Type as a passing parameter in Dart linting

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...