Skip to content

Instantly share code, notes, and snippets.

@OnlyTarg
Last active December 14, 2021 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OnlyTarg/817d8da8d2e0792345441028939eefc4 to your computer and use it in GitHub Desktop.
Save OnlyTarg/817d8da8d2e0792345441028939eefc4 to your computer and use it in GitHub Desktop.
void main() {
//example1 simple use generic class
print('example 1');
GenericExample<String> example1 = GenericExample('SomeString');
GenericExample<int> example2 = GenericExample(123);
print(example1.genericField);
print(example2.genericField);
//example2
print('');
print('example 2');
Function(String s) stringSomeClass = GenericExample.new;
Function(int i) intSomeClass = GenericExample.new;
final genericClass = GenericExample.new;
final a = stringSomeClass('2');
final b = intSomeClass(123);
final c = genericClass(26);
print(a.genericField);
print(b.genericField);
print(c.genericField);
//example 3
print('');
print('example 3');
T id <T> (T value) => value;
var intId1 = id<int>; //2.15
int Function(int) intId2 = id; //pre 2.15
}
class GenericExample<T> {
T genericField;
GenericExample(this.genericField);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment