Reordered class fields?
Jacob Carlborg
doob at me.com
Wed Oct 31 01:23:25 PDT 2012
On 2012-10-31 09:14, Peter Summerland wrote:
> Thanks for the help.
>
> Should the the following example, taken from the D Language Reference,
> be considered incorrect or at least misleading? It clearly depends on
> lexical ordering of the returned fields:
>
> Class Properties
>
> The .tupleof property returns an ExpressionTuple of all the fields in
> the class, excluding the hidden fields and the fields in the base class.
>
> class Foo { int x; long y; }
> void test(Foo foo) {
> foo.tupleof[0] = 1; // set foo.x to 1
> foo.tupleof[1] = 2; // set foo.y to 2
> foreach (x; foo.tupleof)
> writef(x); // prints 12
> }
I would at least consider it bad practice. Even if the compiler
guarantees a specific order it's quite easy for the developer to change
the order of the fields in Foo and then have "test" break.
I would suggest one always access the fields by name. Example:
https://github.com/jacob-carlborg/orange/blob/master/orange/serialization/Serializer.d#L1448
https://github.com/jacob-carlborg/orange/blob/master/orange/util/Reflection.d#L260
> It would be nice if the Language Reference was specific on this point.
> I am aware that the order of the members returned by
> __traits(allMembers, D) is not defined (per the LR). But that is a
> larger, more complex list.
>
> I am just beginning with D, but I think having the tupleof property for
> classes and structs return their fields in lexical order might be useful.
1. It wouldn't hurt if it was clearly specified in the language
specification.
2. You can always sort the list
3. I wouldn't count on the order regardless, see above
> E.g., I have written a "scan" function to load up a struct or class from
> a row returned by a database query. I have, say, scan!"x,y,z"(obj) to
> load data into fields x,y,z of obj. Based on the example above and by
> experimenting, it appears that at least for dmd running on Linux, that
> the fields returned by tupleof are indeed in lexical order. That
> allowed me to have a "" default for the string of field names which
> indicates that all the fields should be loaded. I.e., I have
> scan!""(obj), which can be written scan(obj). Again, it seems to work
> for simple classes and structs with Dmd on Ubuntu.
Indeed, DMD seems to return them in lexical order, but again, I wouldn't
rely on the, see above.
> Don't get me wrong -- I'll be happy without the default version if that
> is the answer. I'm not suggesting any changes.
--
/Jacob Carlborg
More information about the Digitalmars-d-learn
mailing list