Created
August 9, 2017 01:01
-
-
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.
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
| 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