Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "< >" mean in Dart?

Tags:

flutter

dart

Through an online Dart course, I've found some values bracketed with "less than" and "greater than" marks such as "List< E >".

e.g.

List<int> fixedLengthList = new List(5);

I couldn't find a direct answer online, probably because that question was too basic. Could someone explain what those marks exactly indicate? Or any links if possible.

like image 865
Baka Avatar asked Jan 29 '19 17:01

Baka


People also ask

What does ~/ mean in Dart?

~ is currently only used for. ~/ Divide, returning an integer result. and ~/= integer division and assignment.

What does exclamation point mean darts?

"!" is a new dart operator for conversion from a nullable to a non-nullable type. Read here and here about sound null safety. Use it only if you are absolutely sure that the value will never be null and do not confuse it with the conditional property access operator.

What is ~/ operator in Dart?

operator ~/ method Null safetyTruncating division operator. Performs truncating division of this number by other . Truncating division is division where a fractional result is converted to an integer by rounding towards zero. If both operands are ints, then other must not be zero.

What is symbol in Dart?

Symbols in Dart are opaque, dynamic string name used in reflecting out metadata from a library. Simply put, symbols are a way to store the relationship between a human readable string and a string that is optimized to be used by computers.


1 Answers

This are generic type parameters. It allows specializations of classes.

List is a list that can contain any value (if no type parameter is passed dynamic is used by default). List<int> is a list that only allows integer values andnull`.

You can add such Type parameters to your custom classes as well.
Usually single upper-case letters are used for type parameter names like T, U, K but they can be other names like TKey ...

class MyClass<T> {
  T value;
  MyClass(this.value);
}

main() {
  var mcInt = MyClass<int>(5);
  var mcString = MyClass<String>('foo');
  var mcStringError = MyClass<String>(5); // causes error because `5` is an invalid value when `T` is `String`
}

See also https://www.dartlang.org/guides/language/language-tour#generics

like image 84
Günter Zöchbauer Avatar answered Oct 04 '22 13:10

Günter Zöchbauer