www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Unexpected result of IsInstanceOf

reply Ben Jones <fake fake.fake> writes:
The following result doesn't make sense to me... how does 
isInstanceOf return false?

```
import std.traits;
import std.stdio;
import std.typecons;
auto f(T)(T t){
	return Nullable!T(t); 	
}
void main(){
     auto f3 = f(3);
     writeln(typeof(f3).stringof);
     writeln(isInstanceOf!(Nullable, f3));
}
```

outputs

```
Nullable!int
false
```
Jan 30 2020
next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 1/30/20 4:51 PM, Ben Jones wrote:
 The following result doesn't make sense to me... how does isInstanceOf =
 return false?
=20
 ```
 import std.traits;
 import std.stdio;
 import std.typecons;
 auto f(T)(T t){
  =C2=A0=C2=A0=C2=A0=C2=A0return Nullable!T(t);
 }
 void main(){
  =C2=A0=C2=A0=C2=A0 auto f3 =3D f(3);
  =C2=A0=C2=A0=C2=A0 writeln(typeof(f3).stringof);
  =C2=A0=C2=A0=C2=A0 writeln(isInstanceOf!(Nullable, f3));
 }
 ```
=20
 outputs
=20
 ```
 Nullable!int
 false
 ```
In this case it's an instance of a type template, which only types would = be. So, you want to check whether typeof(f3) is an instance of Nullable. Ali
Jan 30 2020
prev sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
On Friday, 31 January 2020 at 00:51:45 UTC, Ben Jones wrote:
     writeln(typeof(f3).stringof);
     writeln(isInstanceOf!(Nullable, f3));
In the first one, you check typeof, but not in the second one. writeln(isInstanceOf!(Nullable, typeof(f3))); // true The template argument there was T above - the type, not the value.
Jan 30 2020