is (T == Specifier)
Let’s discuss the use of the “is” expression when T == Specifier.
We'll cover the following...
It is used to determine whether T is the same type as specifier or whether T matches that specifier.
Whether the same type or not?
Lets consider the example from the previous lesson and use ==
instead of :
, the condition would not be satisfied for AlarmClock
:
Press + to interact
import std.stdio;interface Clock {void tellTime();}class AlarmClock : Clock {override void tellTime() {writeln("10:00");}}void myFunction(T)(T parameter) {static if (is (T == Clock)) {// If we are here then T can be used as a Clockwriteln("This is a Clock; we can tell the time");parameter.tellTime();} else {writeln("This is not a Clock");}}void main() {auto var = new AlarmClock;myFunction(var);myFunction(42);}
Although AlarmClock
is ...