简体   繁体   中英

Dart - Make a function with mandatory type

I have a function in dart

someFunction<T extends SomeClass>() {
  // some code here
}

I want to be mandatory to specify type T

The language specification says ( https://dart.dev/guides/language/spec#h.dkzw3xyky3rs ) :

A type parameter T may be suffixed with an extends clause that specifies the upper bound for T. If no extends clause is present, the upper bound is Object. It is a static type warning if a type parameter is a supertype of its upper bound. The bounds of type variables are a form of type annotation and have no effect on execution in production mode.

So maybe something like the following could work:

<T> somefunction<T extends SomeType>(){}

I have not tested it by myself but let me know if it works.

I think your initial <T> is throwing it off. That should just be T .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM