How do I use null in a struct?

Sebastiaan Koppe mail at skoppe.eu
Sat Nov 10 21:19:54 UTC 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


More information about the Digitalmars-d-learn mailing list