digitalmars.D.learn - Named tuple from struct
- Guilherme Vieira <n2.nitrogen gmail.com> Jan 08 2011
- Jacob Carlborg <doob me.com> Jan 08 2011
--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"'courier new', monospace">struct S { int=
foo; string bar; }</font></div>
<div><font class=3D"Apple-style-span" face=3D"'courier new', monosp=
ace"><br></font></div><div><font class=3D"Apple-style-span" face=3D"'co=
urier new', monospace">S s;</font></div><div><font class=3D"Apple-style=
-span" face=3D"'courier new', monospace"><br>
</font></div><div><font class=3D"Apple-style-span" face=3D"'courier new=
', 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"'c=
ourier new', 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"'courier new', monosp=
ace"><br></font></div><div><font class=3D"Apple-style-span" face=3D"'co=
urier new', monospace">t[0] =3D 1;</font></div><div><font class=3D"Appl=
e-style-span" face=3D"'courier new', monospace">t.bar =3D "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"'courier new', m=
onospace">tuple StructLike</font></div></div><div><font class=3D"Apple-styl=
e-span" face=3D"'courier new', monospace">{</font></div><div><font =
class=3D"Apple-style-span" face=3D"'courier new', monospace">=A0=A0=
=A0int foo;</font></div>
<div><font class=3D"Apple-style-span" face=3D"'courier new', monosp=
ace">=A0=A0 =A0string bar;</font></div><div><font class=3D"Apple-style-span=
" face=3D"'courier new', monospace">}</font></div><div><font class=
=3D"Apple-style-span" face=3D"'courier new', monospace"><br>
</font></div><div><font class=3D"Apple-style-span" face=3D"'courier new=
', monospace">StructLike t;</font></div><div><font class=3D"Apple-style=
-span" face=3D"'courier new', monospace"><br></font></div><div><div=
<font class=3D"Apple-style-span" face=3D"'courier new', monospace"=
t[0] =3D 1;</font></div>
ace">t.bar =3D "2";</font></div></div></blockquote><div><div><br>=
-- <br>Atenciosamente / Sincerely,<br>Guilherme ("n2liquid") Viei=
ra<br>
</div></div>
--001636284880e2740404995154d0--
Jan 08 2011
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#L84Even 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








Jacob Carlborg <doob me.com>