Thread overview | ||||||
---|---|---|---|---|---|---|
|
January 23, 2017 Confirming and uninstantiated struct | ||||
---|---|---|---|---|
| ||||
How do I verify this struct has no value Student getStudent() { ... Student s; if(condition) s = Student; return s; } auto stu = getStudent(); //which will work and is best? if (stu is null) //doesn't wrk. if (stu is Student.init) //will confirm when i get to my pc Or how would you do it? Will you make it null first? (I'm not used to statically typed languages internals) Another issue. I'm creating a function to authenticate user login. I want to determine login failure (Boolean) and error message but D does have multiple return type (could use struct but will make code dirty). in such case, would you use a callback delegates function or will use a string (str == "ok", str == "no") or go with a struct? |
January 24, 2017 Re: Confirming and uninstantiated struct | ||||
---|---|---|---|---|
| ||||
Posted in reply to aberba | On 24/01/2017 2:57 AM, aberba wrote:
> How do I verify this struct has no value
>
> Student getStudent()
> {
> ...
> Student s;
> if(condition) s = Student;
> return s;
> }
>
> auto stu = getStudent();
>
> //which will work and is best?
> if (stu is null) //doesn't wrk.
> if (stu is Student.init) //will confirm when i get to my pc
>
> Or how would you do it? Will you make it null first? (I'm not used to
> statically typed languages internals)
>
> Another issue. I'm creating a function to authenticate user login. I
> want to determine login failure (Boolean) and error message but D does
> have multiple return type (could use struct but will make code dirty).
>
> in such case, would you use a callback delegates function or will use a
> string (str == "ok", str == "no") or go with a struct?
Structs are a value type and will always have a type that won't be null.
If you want it to be nullable you will have to use pointers or classes (there is also Nullable in std.typecons but it won't work with is null).
|
January 24, 2017 Re: Confirming and uninstantiated struct | ||||
---|---|---|---|---|
| ||||
Posted in reply to rikki cattermole | > Structs are a value type and will always have a type that won't be null.
> If you want it to be nullable you will have to use pointers or classes
> (there is also Nullable in std.typecons but it won't work with is null).
s/have a type that won't be null/have a value that won't be null/
My bad.
|
January 23, 2017 Re: Confirming and uninstantiated struct | ||||
---|---|---|---|---|
| ||||
Posted in reply to aberba | On Monday, 23 January 2017 at 13:57:23 UTC, aberba wrote:
> if (stu is Student.init) //will confirm when i get to my pc
That works, as does this:
if(stu == Student.init)
|
Copyright © 1999-2021 by the D Language Foundation