style question on structs

Ali Çehreli acehreli at yahoo.com
Sun Dec 29 21:44:08 PST 2013


On 12/29/2013 10:58 AM, Jonathan wrote:

 > Given a struct
 >
 > struct Foo{
 >    enum INT_C {Yes, No}
 >    INT_C a;
 > }

[...]

 > The second choice is: do we qualify by the struct name:
 >
 > this(int y)
 > {
 >    //option 1
 >    a = Foo.INT_C.Yes;

That's what I do without thinking much about it.

 >    //option 2
 >    a = INT_C.Yes;
 > }

Going off topic a little, Phobos has something to offer for such Yes/No 
flags:

import std.typecons;

struct Foo
{
     Flag!"with_int" a;

     this(int y)
     {
         this.a = Yes.with_int;
     }
}

Yes.with_int is a convenience struct. Otherwise, you can type 
Flag!"with_int".yes and Flag!"with_int".no as well.

Ali



More information about the Digitalmars-d-learn mailing list