How to check if variable of some type can be of null value?

Alexey invalid at email.address
Sat Jul 24 18:10:07 UTC 2021


I've tried to use ```typeof(t) is cast(t)null```, but compiler 
exits with error and so this can't be used for checking this 
issue.

The goal I with to achieve by this check - is to use template and 
to assign value to variable basing on it's ability to accept null 
as a value.

some testing code below.

```D
import std.stdio;

interface I1
{
}

class C1 : I1
{
}

struct S1
{
}

struct S2
{
     int a=1;
}

void main()
{

     auto c1 = new C1;

     I1 i1 = c1;

     auto s1 = S1();
     auto s2 = S2();

     static assert(typeof(c1).init is cast(typeof(c1)) null);
     static assert(typeof(i1).init is cast(typeof(i1)) null);
     static assert(typeof(s1).init is cast(typeof(s1)) null);
     static assert(typeof(s2).init is cast(typeof(s2)) null);
     static assert(int.init is cast(int) null);

}

```


More information about the Digitalmars-d-learn mailing list