-1

This seems like a very obvious question to me, but I cannot seem to find the answer.

How do I write a function type in Dart.

I can use some canned forms like ValueChanged<T> or ValueSetter<T>, but how are they defined? How can I write general function types like T -> U -> V

Jacob Phillips
  • 8,841
  • 3
  • 51
  • 66
Daniel Brotherston
  • 1,954
  • 4
  • 18
  • 28
  • 2
    https://stackoverflow.com/questions/12545762/what-is-a-typedef-in-dart – Jonah Williams Jul 09 '18 at 02:24
  • Thanks for making the duplicate, I never would have expected it to be bolted into typedefs. I am assuming then there is no way to express an unnamed function type without using a typedef? – Daniel Brotherston Jul 09 '18 at 15:21
  • 2
    There is syntax without typedefs. See https://www.dartlang.org/guides/language/effective-dart/design#prefer-inline-function-types-over-typedefs – Kevin Moore Aug 02 '18 at 03:15

1 Answers1

-3

If I understood your question correctly, I think you are referring to Generic Types. These types are used by functions when you are not sure which type your function is going to use in order to avoid code duplication. For example, let's say you have a class with a method that stores numbers in a List:

class StoreInt {
  storeInList(int value);
}

But later, you decide you now want it to store Strings, so you create another class:

class StoreString {
   storeInList(String value);
 }

Generics allow you to write this class once and use it to store any kind of object you want:

class Store<T> {
  storeInList(T value);
}

As far as I know, you don't create generics in dart, you just specify a single letter in between diamond operators as shown in the previous example.

You can find more information on generics here: https://www.dartlang.org/guides/language/language-tour#generics

Jacob Soffer
  • 304
  • 1
  • 2
  • 13