Comparison of struct with Nullable member

Meta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jul 17 09:03:29 PDT 2015


On Friday, 17 July 2015 at 15:52:45 UTC, TC wrote:
> On Friday, 17 July 2015 at 15:30:42 UTC, Meta wrote:
>> https://issues.dlang.org/show_bug.cgi?id=14804
>>
>> I'll probably be able to submit a PR for this sometime in the 
>> next few days.
>
> Thanks.
> What I don't get is why this one works ok?
>
> import std.typecons : Nullable;
> struct Foo
> {
> 	int bar;
> 	Nullable!int baz;
> }
> auto a = Foo(1);
> auto b = Foo(1);
> assert(a == b);

This may be because if your struct only contains primitive 
values, the compiler will just do a memcmp to determine equality. 
Don't quote me on that though.

> Also is there some "nicer" way to init the struct with nullable 
> member with default constructor?
> Now I'm using Foo(1, Nullable!int(2)) but just Foo(1, 2) would 
> be much nicer.
> Same with calling functions with nullable params.

You can defined a custom constructor in Foo which will construct 
the Nullable.

struct Foo
{
     this(int n1, int n2)
     {
         bar = n1;
         baz = n2;
     }

     //...
}


More information about the Digitalmars-d-learn mailing list