Last active
September 19, 2021 03:30
-
-
Save riscait/f403ec0022a770f16fb50daba96b40a0 to your computer and use it in GitHub Desktop.
Dart Generics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Cat { | |
| const Cat(this.name); | |
| final String name; | |
| } | |
| void f<T>(T arg) { | |
| if (arg is String) { | |
| print('This is String!'); | |
| } | |
| if (arg is int) { | |
| print('This is int!'); | |
| } | |
| if (arg is Cat) { | |
| print('This is Cat!'); | |
| print('Cat name is ${arg.name}'); | |
| } | |
| print('arg is String: ${arg is String}'); | |
| print('arg == String: ${arg == String}'); | |
| print('T is String: ${T is String}'); | |
| print('T == String: ${T == String}'); | |
| print('arg is int: ${arg is int}'); | |
| print('arg == int: ${arg == int}'); | |
| print('T is int: ${T is int}'); | |
| print('T == int: ${T == int}'); | |
| print('arg is Cat: ${arg is Cat}'); | |
| print('arg == Cat: ${arg == Cat}'); | |
| print('T is Cat: ${T is Cat}'); | |
| print('T == Cat: ${T == Cat}'); | |
| } | |
| void main() { | |
| print('String'); | |
| f('moji'); | |
| print('---'); | |
| print('int'); | |
| f(123); | |
| print('---'); | |
| print('Cat'); | |
| f(Cat('pinkie')); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment