www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Tuple opAssign type deduction

reply "aldanor" <i.s.smirnov gmail.com> writes:
alias T = Tuple!(int, "a", double, "b");
T foo = [1, 2]; // works
T bar;
bar = [1, 2]; // doesn't?

Wonder if there's an obvious reason to this?
Dec 23 2014
next sibling parent ketmar via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Wed, 24 Dec 2014 00:16:33 +0000
aldanor via Digitalmars-d <digitalmars-d puremagic.com> wrote:

 alias T =3D Tuple!(int, "a", double, "b");
 T foo =3D [1, 2]; // works
 T bar;
 bar =3D [1, 2]; // doesn't?
=20
 Wonder if there's an obvious reason to this?
you mean "other than the fact that `T foo =3D [1, 2];` is initalization, not assigning"? ;-) you just stepped into two operations that looking almost the same, but does very different things. the thing is that Tuple supports assigning only from another Tuple. it is possible to build `opAssign` templates to support the case you shown, but it will be cumbersome and will bloat the code. tl;dr: nobody wrote the proper `opAssign` generator. ;-)
Dec 23 2014
prev sibling next sibling parent ketmar via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Wed, 24 Dec 2014 00:16:33 +0000
aldanor via Digitalmars-d <digitalmars-d puremagic.com> wrote:

 alias T =3D Tuple!(int, "a", double, "b");
 T foo =3D [1, 2]; // works
 T bar;
 bar =3D [1, 2]; // doesn't?
=20
 Wonder if there's an obvious reason to this?
to clarify "different operations" a little. take a look at this code: import std.stdio; struct S { string v; this (string vv) { writeln("ctor!"); v =3D vv; } void opAssign (string vv) { writeln("assign!"); v =3D vv; } } void main () { S a =3D "hello"; // (1) outputs "ctor!" S b; b =3D "hey!"; // (2) outputs "assign!" } see, (1) is transformed to: `S a =3D S("hello");`. so your first case is calling Tuple ctor, and your second case is calling Tuple `opAssign`. Tuple ctor is defined like this: /** * Constructor taking a compatible array. * * Examples: * ---- * int[2] ints; * Tuple!(int, int) t =3D ints; * ---- */ this(U, size_t n)(U[n] values) if (n =3D=3D Types.length && allSatisfy!(isBuildableFrom!U, Types)) and Type `opAssign` is defined like this: void opAssign(R)(auto ref R rhs) if (areCompatibleTuples!(typeof(this), R, "=3D")) i.e. we can construct Tuple from array, but can't assign array to Tuple.
Dec 23 2014
prev sibling parent reply ketmar via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Wed, 24 Dec 2014 00:16:33 +0000
aldanor via Digitalmars-d <digitalmars-d puremagic.com> wrote:

 alias T =3D Tuple!(int, "a", double, "b");
 T foo =3D [1, 2]; // works
 T bar;
 bar =3D [1, 2]; // doesn't?
=20
 Wonder if there's an obvious reason to this?
as for "why it's not working"... i honestly don't know. adding simple `opAssign` overload to Tuple struct solves it: void opAssign(U, size_t n)(U[n] values) if (n =3D=3D Types.length && allSatisfy!(isBuildableFrom!U, Types)) { foreach (i, _; Types) { field[i] =3D values[i]; } } i guess that this was simply overlooked, so i think that you can create ER for this.
Dec 23 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
ketmar:

 i guess that this was simply overlooked, so i think that you 
 can create ER for this.
Often tuple fields have different types, like a string and int. And better to stop and wait for built-in tuple syntax instead of messing up more and more the Phobos tuples. The rabbit hole is deep enough already. Bye, bearophile
Dec 23 2014
parent ketmar via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Wed, 24 Dec 2014 01:00:08 +0000
bearophile via Digitalmars-d <digitalmars-d puremagic.com> wrote:

 i guess that this was simply overlooked, so i think that you=20
 can create ER for this.
=20 Often tuple fields have different types, like a string and int.
sure, but then you can't initialize it from an array either. yet we have convient initializer for "convertable" arrays and have no `opAssign` for the same.
 And better to stop and wait for built-in tuple syntax instead of=20
 messing up more and more the Phobos tuples. The rabbit hole is=20
 deep enough already.
built-in tuple syntax may not arrive for another two years. or five. or... ;-) adding `opAssign` for arrays is just a convience feature: as we have ctor for this operation, it would be logical to have `opAssign` for it too.
Dec 23 2014