Skip to content

Instantly share code, notes, and snippets.

@schultek
Last active November 1, 2024 10:26
Show Gist options
  • Save schultek/e805249b2788095f4a43cd51c100f4de to your computer and use it in GitHub Desktop.
Save schultek/e805249b2788095f4a43cd51c100f4de to your computer and use it in GitHub Desktop.
Subtype challenge
// TODO: Implement the [isSubtype] function. Must be a general solution, no hardcoding stuff!
// Check if [A] is a subtype of [B], according to https://dart.dev/resources/glossary#subtype
// For example:
// - int is a subtype of num
// - List<int> is a subtype of List<num>
// - Labrador is a subtype of Animal
bool isSubtype<A, B>() {
// TODO: Implement this!
}
// ======== VALIDATION =======
void main() {
assert(isSubtype<int, num>());
assert(isSubtype<String, Object>());
assert(isSubtype<List<int>, List<dynamic>>());
assert(isSubtype<List<int>, List<num>>());
assert(!isSubtype<List<num>, List<int>>());
assert(isSubtype<Animal, dynamic>());
assert(isSubtype<Cat, Animal>());
assert(isSubtype<Dog, Animal>());
assert(!isSubtype<Cat, Dog>());
assert(isSubtype<Labrador, Dog>());
assert(isSubtype<Labrador, Animal>());
assert(!isSubtype<Animal, Labrador>());
print("Yayy Success! 🎉");
}
// Helper Types
class Animal {}
class Cat implements Animal {}
class Dog extends Animal {}
class Labrador extends Dog {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment