Last active
January 31, 2021 06:00
-
-
Save brandondiamond/667ba7a0f3056eb5582e11dd6269f35d to your computer and use it in GitHub Desktop.
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
class Identifiable<T> { | |
Id<T> id; | |
} | |
class Id<T> {} | |
class Person extends Identifiable<Person> {} | |
class Animal extends Identifiable<Animal> {} | |
class Resolver<T extends Identifiable> { | |
void resolve(dynamic arg, void Function<V extends Identifiable>(dynamic) func) { | |
func<T>(arg); | |
} | |
} | |
final dynamic resolvers = { | |
Id<Person>().runtimeType: Resolver<Person>(), | |
Id<Animal>().runtimeType: Resolver<Animal>() | |
}; | |
void reveal<T extends Identifiable>(Id<T> id) { | |
print("Static: ${Id<T>().runtimeType}"); | |
print("Dynamic: ${id.runtimeType}\n"); | |
} | |
void resolve(dynamic arg, void Function<V extends Identifiable>(dynamic) func) { | |
resolvers[arg.runtimeType].resolve(arg, func); | |
} | |
main() { | |
dynamic lst = []; | |
lst.add(Id<Person>()); | |
lst.add(Id<Animal>()); | |
/* | |
Static: Id<Identifiable<dynamic>> | |
Dynamic: Id<Person> | |
*/ | |
reveal(lst[0]); | |
/* | |
Static: Id<Person> | |
Dynamic: Id<Person> | |
*/ | |
resolve(lst[0], <V extends Identifiable>(arg){ | |
reveal<V>(arg); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment