www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Named tuple from struct

reply Guilherme Vieira <n2.nitrogen gmail.com> writes:
--001636284880e2740404995154d0
Content-Type: text/plain; charset=ISO-8859-1

Is is possible to get a named tuple from a struct type?

E.g.:

struct S { int foo; string bar; }

S s;

S.tupleof t; // S.tupleof is a tuple type, as opposed to s.tupleof,
             // which yields a tuple instance

t[0] = 1;
t.bar = "2";


If not, I think it would be quite useful.

Even still, a way to describe tuple types as if it was a struct would also
be useful:

tuple StructLike
{
    int foo;
    string bar;
}

StructLike t;

t[0] = 1;
t.bar = "2";


-- 
Atenciosamente / Sincerely,
Guilherme ("n2liquid") Vieira

--001636284880e2740404995154d0
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Is is possible to get a named tuple from a struct type?<div><br></div><div>=
E.g.:</div><div><br></div><blockquote class=3D"webkit-indent-blockquote" st=
yle=3D"margin: 0 0 0 40px; border: none; padding: 0px;"><div><font class=3D=
"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">struct S { int=
 foo; string bar; }</font></div>
<div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monosp=
ace"><br></font></div><div><font class=3D"Apple-style-span" face=3D"&#39;co=
urier new&#39;, monospace">S s;</font></div><div><font class=3D"Apple-style=
-span" face=3D"&#39;courier new&#39;, monospace"><br>
</font></div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new=
&#39;, monospace">S.tupleof t; // S.tupleof is a tuple type, as opposed to =
s.tupleof,</font></div><div><font class=3D"Apple-style-span" face=3D"&#39;c=
ourier new&#39;, monospace">=A0=A0 =A0 =A0 =A0 =A0 =A0 // which yields a tu=
ple instance</font></div>
<div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monosp=
ace"><br></font></div><div><font class=3D"Apple-style-span" face=3D"&#39;co=
urier new&#39;, monospace">t[0] =3D 1;</font></div><div><font class=3D"Appl=
e-style-span" face=3D"&#39;courier new&#39;, monospace">t.bar =3D &quot;2&q=
uot;;</font></div>
</blockquote><div><div><br></div><div>If not, I think it would be quite use=
ful.</div><div><br></div><div>Even still, a way to describe tuple types as =
if it was a struct would also be useful:</div><div><br></div></div><blockqu=
ote class=3D"webkit-indent-blockquote" style=3D"margin: 0 0 0 40px; border:=
 none; padding: 0px;">
<div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, m=
onospace">tuple StructLike</font></div></div><div><font class=3D"Apple-styl=
e-span" face=3D"&#39;courier new&#39;, monospace">{</font></div><div><font =
class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace">=A0=A0=
 =A0int foo;</font></div>
<div><font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monosp=
ace">=A0=A0 =A0string bar;</font></div><div><font class=3D"Apple-style-span=
" face=3D"&#39;courier new&#39;, monospace">}</font></div><div><font class=
=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace"><br>
</font></div><div><font class=3D"Apple-style-span" face=3D"&#39;courier new=
&#39;, monospace">StructLike t;</font></div><div><font class=3D"Apple-style=
-span" face=3D"&#39;courier new&#39;, monospace"><br></font></div><div><div=
<font class=3D"Apple-style-span" face=3D"&#39;courier new&#39;, monospace"=
t[0] =3D 1;</font></div>

ace">t.bar =3D &quot;2&quot;;</font></div></div></blockquote><div><div><br>= -- <br>Atenciosamente / Sincerely,<br>Guilherme (&quot;n2liquid&quot;) Viei= ra<br> </div></div> --001636284880e2740404995154d0--
Jan 08 2011
parent Jacob Carlborg <doob me.com> writes:
On 2011-01-08 09:15, Guilherme Vieira wrote:
 Is is possible to get a named tuple from a struct type?

 E.g.:

     struct S { int foo; string bar; }

     S s;

     S.tupleof t; // S.tupleof is a tuple type, as opposed to s.tupleof,
                   // which yields a tuple instance

     t[0] = 1;
     t.bar = "2";


 If not, I think it would be quite useful.

You cannot get only the names as a tuple but you can implement something similar: 1. Loop through the tuple 2. Get the name of a field in the struct using the "stringof" property on the tuple 3. Slice of the name of the struct and some other unwanted characters You can have a look at this method as well: http://dsource.org/projects/dstep/browser/dstep/objc/bridge/ClassInitializer.d#L84
 Even still, a way to describe tuple types as if it was a struct would
 also be useful:

     tuple StructLike
     {
          int foo;
          string bar;
     }

     StructLike t;

     t[0] = 1;
     t.bar = "2";


 --
 Atenciosamente / Sincerely,
 Guilherme ("n2liquid") Vieira

-- /Jacob Carlborg
Jan 08 2011