Struct problems
Maxim Fomin
maxim at maxim-fomin.ru
Mon Sep 17 11:47:54 PDT 2012
I consider current struct creation one of the confusing parts of
the language (may be the most), due to set of incompatible
creation semantics masked by same syntax, complicated by couple
of semi-bugs (7210, 1310, 4053) and naive default arguments
embedding into the language(3438).
Current creation rules look as follows
(http://dpaste.dzfl.pl/a4344ad0):
1) if a struct object declaration has no initializer, its members
are default initialized to either explicit initializer in struct
declaration or type default initializer (note: actually, there
are no implicit struct constructors). If it has void initializer,
it contains garbage
2) otherwise, if it has initialization form S() than:
a) if opCall member is present, it is called (must have no
arguments)
b) otherwise, initialization proceed according to 1)
3) otherwise, if it has initialization form S(T1 arg1, ...), than:
a) if opCall member is present, it is called (its parameters must
be consistent with arguments)
b) otherwise, if at least one ctor exists, it is called (and
again, its parameters must be consistent with arguments)
c) otherwise, this initialization form is called struct literal
and struct members are initialized to arguments in accordance
with their order in struct declaration
This means that if you have S(), or S(x, y, x) - it is impossible
to know without looking into struct definition what is going on:
a function call (constructor or opCall) or just initialization.
If naive default argument treatment is considered, I may add that
there is no sense of setting default argument to one-argument
struct constructor, since S()-like expression would call
something else (3438). By the way, .init property may be hijacked
(good news is that it doesn't affect default initializer).
Basically, the question is, is it considered to be good, well
designed feature or not. And if not, would it be changed at some
time? What was original design of structures in D?
More information about the Digitalmars-d
mailing list