Created
January 24, 2020 15:38
-
-
Save AlexVegner/e337d7a801ba8dab7d4a0e91f20af465 to your computer and use it in GitHub Desktop.
dart_generic.dart
This file contains 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
abstract class Cache<T> { | |
T getByKey(String key); | |
void setByKey(String key, T value); | |
} | |
class StringCashe extends Cache<String> { | |
var _cache = Map<String, String>(); | |
@override | |
String getByKey(String key) { | |
return _cache[key]; | |
} | |
@override | |
void setByKey(String key, String value) { | |
_cache[key] = value; | |
} | |
} | |
/// Generic implementation of cache | |
class GeneralCache<T> implements Cache<T> { | |
var _cache = Map<String, T>(); | |
@override | |
T getByKey(String key) { | |
return _cache[key]; | |
} | |
@override | |
void setByKey(String key, T value) { | |
_cache[key] = value; | |
} | |
} | |
/// T extends from num | |
class NumberCache<T extends num> extends GeneralCache<T> {} | |
/// Generic function typedef | |
typedef TypedCallback = void Function<T>(T value); | |
/// Generic method | |
List<T> merge<T>(List<T> list1, List<T> list2) { | |
return [ | |
...list1, | |
...list2, | |
]; | |
} | |
/// Generic extension | |
extension ListExt<T> on List<T> { | |
/// Generic extension method | |
List<T> merge(List<T> ts) { | |
return [ | |
...this, | |
...ts, | |
]; | |
} | |
} | |
void main() { | |
final list1 = <int>[1, 2, 3]; // type check | |
final list2 = <int>[5, 6, 7]; | |
print(list1.merge(list2)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment