How do I check if a type is assignable to null at compile time?

Nathan S. no.public.email at example.com
Fri Feb 26 05:36:27 UTC 2021


On Friday, 26 February 2021 at 05:25:14 UTC, Jack wrote:
> I started with:
>
> enum isAssignableNull(T) = is(T : Object) || isPointer(T);
>
> but how do I cover all cases?

If I understand what you mean by "is assignable to null", this 
should do it:

---
enum bool isAssignableNull(T) = is(typeof(null) : T);

// Tests:

immutable string arrayslice = null;
static assert(isAssignableToNull!(immutable string));

void function(int) funptr = null;
static assert(isAssignableToNull!(typeof(funptr)));

int[int] aa = null;
static assert(isAssignableToNull!(int[int]));
---



More information about the Digitalmars-d-learn mailing list