www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19036] New: .tupleof order guarantee

https://issues.dlang.org/show_bug.cgi?id=19036

          Issue ID: 19036
           Summary: .tupleof order guarantee
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dlang.org
          Assignee: nobody puremagic.com
          Reporter: gsub glat.info

Hello,

currently there is no official guarantee on the order returned by .tupleof:
https://dlang.org/spec/class.html#class_properties

I have a use case where I'd like to serialize most fields of a class, *in their
declaration order*, into a byte array. The fields are either simple arithmetic
types, or dynamic arrays of arithmetic types, so I am currently doing something
like this:

class A {
  int x,y,z;
  int _internal_use_only;
  int[] arr;

  void packTo( ref ubyte[] result )
  {
    result.length = 0;
    static foreach( i, field; A.tupleof )  // <<< HERE
    {
      static if (!__traits( identifier, field ).startsWith( "_" ))
        {
          {
            ubyte[] b;
            static if (__traits( isArithmetic, typeof( field ) ))
              {
                b ~= nativeToLittleEndian( field );
              }
            else
              {
                foreach( v; field )
                  b ~= nativeToLittleEndian( v );
              }
            result ~= b;
          }
        }
    }
  }
}

Since in my use case, the serialization order MUST match a spec, it is
important to have a guarantee on the .tupleof order.

The current implementations already seem to guarantee that order. Would it be
thinkable to have the guarantee in the spec as well?

Note: I found a previous discussion on this, but no issue:
https://forum.dlang.org/post/mailman.60.1478908571.9448.digitalmars-d-learn puremagic.com

Thanks!

Guillaume Lathoud

--
Jun 27 2018