digitalmars.D.learn - How do I check if a type is assignable to null at compile time?
- Jack (3/3) Feb 25 2021 I started with:
- Paul Backus (3/6) Feb 25 2021 Something like this should work:
- Nathan S. (4/13) Feb 25 2021 `isAssignableNull!(immutable void*)` is true with his definition
- Jack (3/18) Feb 26 2021 yep, it must be true for pointers too. Thank you all guys.
- Nathan S. (13/16) Feb 25 2021 If I understand what you mean by "is assignable to null", this
- Murilo (3/6) Feb 26 2021 You can check if it's null with this `variable is null` and you
- Jack (2/10) Feb 26 2021 I mean a give type T not variablee
- H. S. Teoh (7/19) Feb 26 2021 Why not just:
- Jack (4/22) Feb 28 2021 works too, thanks but I ended up using Nathan S. solution which
I started with: enum isAssignableNull(T) = is(T : Object) || isPointer(T); but how do I cover all cases?
Feb 25 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);
Feb 25 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:`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.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
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:yep, it must be true for pointers too. Thank you all guys. is(typeof(null) : T) works like a charmOn Friday, 26 February 2021 at 05:25:14 UTC, Jack wrote:`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.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 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])); ---
Feb 25 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);`
Feb 26 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 mean a give type T not variableeI 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
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:Why not just: enum isAssignableNull(T) = is(typeof((T t){ t = null; })); ? T -- What are you when you run out of Monet? Baroque.On Friday, 26 February 2021 at 05:25:14 UTC, Jack wrote:I mean a give type T not variableeI 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
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:works too, thanks but I ended up using Nathan S. solution which is quite fine too: enum bool isAssignableNull(T) = is(typeof(null) : T);On Friday, 26 February 2021 at 23:37:18 UTC, Murilo wrote:Why not just: enum isAssignableNull(T) = is(typeof((T t){ t = null; })); ? TOn Friday, 26 February 2021 at 05:25:14 UTC, Jack wrote:I mean a give type T not variableeI 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 28 2021