struct initialization and assignment by field name
    bearophile 
    bearophileHUGS at lycos.com
       
    Sat Dec  1 11:32:26 PST 2012
    
    
  
Bobby Bingham:
> It appears that this form of struct literal really can only be 
> used in initializers -- the assignment to a previously declared 
> varaible fails to compile.
Right. That syntax is not much used in D, there were discussions 
about deprecating it fully, I don't know the current status. I 
use it sometimes when I have to initialize an array of many 
structs.
> I'm hoping somebody can shed some light on the rationale for 
> only supporting this form of struct literal in initializers.
I don't know the rationale. There are tons of things I don't know 
the rationale of, despite my efforts to learn.
The normal way to write a struct literal in D is this, that works 
in most cases:
auto foo = MyStruct(42, 'a');
> And also why it's documented to only work for static
> initializers -- is this an error in the documentation,
> or is the compiler allowing things it shouldn't?
It's another little mystery :-) Maybe the DMD compiler used to 
allow that, and then specs were updated and the compiler remained 
unchanged, or the specs where like that since the beginning, but 
the compiler was closer to a C99 one and allowed it. Or maybe 
it's just a compiler bug that allows something that is not 
allowed. Maybe it's Bugzilla worth.
Bye,
bearophile
    
    
More information about the Digitalmars-d-learn
mailing list