Thread overview
How do I check if a type is assignable to null at compile time?
Feb 26, 2021
Jack
Feb 26, 2021
Paul Backus
Feb 26, 2021
Nathan S.
Feb 26, 2021
Jack
Feb 26, 2021
Nathan S.
Feb 26, 2021
Murilo
Feb 27, 2021
Jack
Feb 27, 2021
H. S. Teoh
Mar 01, 2021
Jack
February 26, 2021
I started with:

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

but how do I cover all cases?
February 26, 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?

Something like this should work:

enum isAssignableNull(T) = __traits(compiles, (T t) => t = null);
February 26, 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]));
---

February 26, 2021
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.
February 26, 2021
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
February 26, 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?

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);`
February 27, 2021
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
February 26, 2021
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.
March 01, 2021
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);