What is typedefs in DART
In this article, i am going to explain about typesdefs.
Typedefs in DART
A DART typedefs, or function type alias, gives a function name that you can use when declaring field and return type. A typedef contains information when a function type is assign a variable.
Example of DART typedefs
typedef int Check(Object a, Object b);
class Darttypedef {
Check compare;
Darttypedef(this.compare);
}
int sort(Object a, Object b) => 0;
main() {
Darttypedef collection = new Darttypedef(sort);
if(collection.compare is Function){
print(true);
}
if(collection.compare is Check){
print(true);
}
}
|
Output:
- You cal also use generic typedefs:
Example of generic typedefs
typedef int Check<T>(T a, T b);
class TypedefDart<T> {
Check<T> compare;
TypedefDart(this.compare);
}
main() {
TypedefDart<int> s = new TypedefDart<int>((a,b) => a - b);
if(s.compare is Check<int>){
print(true);
}
}
|
Output:
Ask Your Question
Got a programming related question? You may want to post your question here