www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How do I use null in a struct?

reply =?UTF-8?B?VsOhY2xhdiBLb3rDoWs=?= <sudoman281 gmail.com> writes:
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.
Nov 10 2018
next sibling parent drug <drug2004 bk.ru> writes:
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
Nov 10 2018
prev sibling next sibling parent Sebastiaan Koppe <mail skoppe.eu> writes:
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
Nov 10 2018
prev sibling parent Heromyth <bitworld qq.com> writes:
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.
Nov 11 2018