Thread overview
How do I use null in a struct?
Nov 10, 2018
Václav Kozák
Nov 10, 2018
drug
Nov 10, 2018
Sebastiaan Koppe
Nov 12, 2018
Heromyth
November 10, 2018
I'm making a Rest API with vibe.d and I have a struct User. Sometimes I need to return only a few of the fields. So for example: return User(1, null, "John", null, null, ...);
If I do this, an error occurs: cannot implicitly convert expression null of type typeof(null) to ...
Thanks.
November 10, 2018
On 10.11.2018 22:42, Václav Kozák wrote:
> I'm making a Rest API with vibe.d and I have a struct User. Sometimes I need to return only a few of the fields. So for example: return User(1, null, "John", null, null, ...);
> If I do this, an error occurs: cannot implicitly convert expression null of type typeof(null) to ...
> Thanks.
Probably you need Nullable? https://dlang.org/phobos/std_typecons.html#Nullable
November 10, 2018
On Saturday, 10 November 2018 at 19:42:47 UTC, Václav Kozák wrote:
> I'm making a Rest API with vibe.d and I have a struct User. Sometimes I need to return only a few of the fields. So for example: return User(1, null, "John", null, null, ...);
> If I do this, an error occurs: cannot implicitly convert expression null of type typeof(null) to ...
> Thanks.

You can use Optional like drug said, or you can use the @optional attribute [1].

@optional will leave the field in its .init state, whereas with Optional its easier to detect the null state.

[1] http://vibed.org/api/vibe.data.serialization/optional
November 12, 2018
On Saturday, 10 November 2018 at 19:42:47 UTC, Václav Kozák wrote:
> I'm making a Rest API with vibe.d and I have a struct User. Sometimes I need to return only a few of the fields. So for example: return User(1, null, "John", null, null, ...);
> If I do this, an error occurs: cannot implicitly convert expression null of type typeof(null) to ...
> Thanks.

Another choice: https://github.com/huntlabs/hunt/blob/master/source/hunt/lang/Nullable.d

This Nullable is a class and we are using it.