www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How do I check if a type is assignable to null at compile time?

reply Jack <jckj33 gmail.com> writes:
I started with:

enum isAssignableNull(T) = is(T : Object) || isPointer(T);

but how do I cover all cases?
Feb 25 2021
next sibling parent reply Paul Backus <snarwin gmail.com> writes:
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?
Something like this should work: enum isAssignableNull(T) = __traits(compiles, (T t) => t = null);
Feb 25 2021
parent reply Nathan S. <no.public.email example.com> writes:
On Friday, 26 February 2021 at 05:34:26 UTC, Paul Backus wrote:
 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?
Something like this should work: enum isAssignableNull(T) = __traits(compiles, (T t) => t = null);
`isAssignableNull!(immutable void*)` is true with his definition but false with yours. Of course you are correct that you cannot assign to an immutable pointer.
Feb 25 2021
parent Jack <jckj33 gmail.com> writes:
On Friday, 26 February 2021 at 05:45:39 UTC, Nathan S. wrote:
 On Friday, 26 February 2021 at 05:34:26 UTC, Paul Backus wrote:
 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?
Something like this should work: enum isAssignableNull(T) = __traits(compiles, (T t) => t = null);
`isAssignableNull!(immutable void*)` is true with his definition but false with yours. Of course you are correct that you cannot assign to an immutable pointer.
yep, it must be true for pointers too. Thank you all guys. is(typeof(null) : T) works like a charm
Feb 26 2021
prev sibling next sibling parent Nathan S. <no.public.email example.com> writes:
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])); ---
Feb 25 2021
prev sibling parent reply Murilo <murilomiranda92 hotmail.com> writes:
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?
You can check if it's null with this `variable is null` and you can test it with assert as in `assert(variable is null);`
Feb 26 2021
parent reply Jack <jckj33 gmail.com> writes:
On Friday, 26 February 2021 at 23:37:18 UTC, Murilo wrote:
 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?
You can check if it's null with this `variable is null` and you can test it with assert as in `assert(variable is null);`
I mean a give type T not variablee
Feb 26 2021
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sat, Feb 27, 2021 at 01:03:56AM +0000, Jack via Digitalmars-d-learn wrote:
 On Friday, 26 February 2021 at 23:37:18 UTC, Murilo wrote:
 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?
You can check if it's null with this `variable is null` and you can test it with assert as in `assert(variable is null);`
I mean a give type T not variablee
Why not just: enum isAssignableNull(T) = is(typeof((T t){ t = null; })); ? T -- What are you when you run out of Monet? Baroque.
Feb 26 2021
parent Jack <jckj33 gmail.com> writes:
On Saturday, 27 February 2021 at 01:23:06 UTC, H. S. Teoh wrote:
 On Sat, Feb 27, 2021 at 01:03:56AM +0000, Jack via 
 Digitalmars-d-learn wrote:
 On Friday, 26 February 2021 at 23:37:18 UTC, Murilo wrote:
 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?
You can check if it's null with this `variable is null` and you can test it with assert as in `assert(variable is null);`
I mean a give type T not variablee
Why not just: enum isAssignableNull(T) = is(typeof((T t){ t = null; })); ? T
works too, thanks but I ended up using Nathan S. solution which is quite fine too: enum bool isAssignableNull(T) = is(typeof(null) : T);
Feb 28 2021