Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the dart function type syntax for variable declaration?

Tags:

dart

I know you can specify function types in formal arg list, but how would I do this for instance variables? I would like to do this:

class A<T> {   int compare(T a, T b); } 

where compare is a function variable with the appropriate type. I would like to be able to write:

A a = new A(); a.compare = ... 
like image 920
jz87 Avatar asked Jul 12 '13 14:07

jz87


People also ask

How do you declare a variable in darts?

How to Declare Variable in Dart. We need to declare a variable before using it in a program. In Dart, The var keyword is used to declare a variable. The Dart compiler automatically knows the type of data based on the assigned to the variable because Dart is an infer type language.

What is function type in Dart?

There are four main types of user define functions (based on arguments and return type). Function with no arguments and no return type. Function with arguments and no return type. Function with no arguments and return type. Function with arguments and with return type.

How do you get variable type in Dart?

To check the type of a variable in Flutter and Dart, you can use the runtimeType property.


1 Answers

You can use typedef :

typedef Comparison<T> = int Function(T a, T b);  class A<T> {   Comparison<T> compare; }  main() {   A a = new A<int>();   a.compare = (int a, int b) => a.compareTo(b);   print(a.compare(1, 2)); } 
like image 70
Alexandre Ardhuin Avatar answered Oct 18 '22 10:10

Alexandre Ardhuin