Skip to content

Instantly share code, notes, and snippets.

@JesseKPhillips
Created August 9, 2017 01:01
Show Gist options
  • Select an option

  • Save JesseKPhillips/a0a0b3d891607bd4f7352fc6febc7deb to your computer and use it in GitHub Desktop.

Select an option

Save JesseKPhillips/a0a0b3d891607bd4f7352fc6febc7deb to your computer and use it in GitHub Desktop.
Shows a couple of different ways to check at compile time that a composed class inherits specific interfaces.
void main() {}
class Provider {
Concrete c;
}
class Concrete : HasThis, HasThat { }
interface HasThis {}
interface HasThat {}
void m(T)(T t) if(is(typeof(T.c) : HasThis)) {
}
class Provider2 : C{
Concrete c;
override Concrete getC() { return c; }
}
interface C {
HasThat getC();
}
void m2(T : C)(T t) {
}
void foo() {
m(Provider.init);
m2(Provider2.init);
}
// This is a Compiler Error
class Provider3 : C {
Concrete2 c;
override Concrete2 getC() { return c; }
}
class Concrete2 : HasThis { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment