On Saturday, 1 December 2012 at 19:32:27 UTC, bearophile wrote:
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
I'm starting to look into this kind of thing now, although I ave
not used struct initializations like that yet for real code, just
for my tinkering.
In my case I've been fully relying on constructors and
initializers, and that seems to be working very well for me, with
no reason to change.
If you want to perform initializations like that, there is one
advantage of specifying the field name:
If the struct fields are reordered, then that will not require
changes elsewhere, also for example, if you reorder two ints, the
code will compile, but will create difficult to detect errors
when run.
struct X{
int a, b;
}
X x = { a: 12, b: 13 }
------
struct X{
int b, a;
}
X x = { 12, 13 }
Personally, I think you are far better off using constructors,
overloaded opAssign and so forth.
Now if you really want to dive into the weirdness of D, then have
a look at tuples. Using a tuple, you can perform similar named
initializations, and more, but it's far too weird and messy for
me to make real use out of, i.e., it is in dire need of a
re-think and redesign to make it a clean generalized concept that
fits in consistently with the other components of the language.
--rt