style question on structs

John Colvin john.loughran.colvin at gmail.com
Sun Dec 29 11:51:42 PST 2013


On Sunday, 29 December 2013 at 18:58:07 UTC, Jonathan wrote:
> Given a struct
>
> struct Foo{
>   enum INT_C {Yes, No}
>   INT_C a;
> }
>
> -- we wish to make a constructor.  We want to set the answer to 
> Yes if we construct the struct with an integer.  There are some 
> stylistic choices to be made.
>
> The first choice is: do we use this (I seem to recall being 
> told in Java with classes there is debate whether 'this' is 
> preferrable or not).
>
> this(int y)
> {
>   //choice 1:
>   this.a = this.INT_C.Yes;
>   //choice 2:
>   a = INT_C.Yes;
> }
>
> The second choice is: do we qualify by the struct name:
>
> this(int y)
> {
>   //option 1
>   a = Foo.INT_C.Yes;
>   //option 2
>   a = INT_C.Yes;
> }
>
> The reason I ask, is that I remember some cases in Java where 
> it was advantageous to use 'this'.

There is no need to qualify by either of them.

If you do want to qualify for readability reasons then "this" 
would be preferable in my opinion as it will remain correct if 
you later change Foo to a templated struct.


More information about the Digitalmars-d-learn mailing list