On Wed, 24 Dec 2014 00:16:33 +0000 aldanor via Digitalmars-d <[email protected]> wrote:
> 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?
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 = vv; }
void opAssign (string vv) { writeln("assign!"); v = vv; }
}
void main () {
S a = "hello"; // (1) outputs "ctor!"
S b;
b = "hey!"; // (2) outputs "assign!"
}
see, (1) is transformed to: `S a = 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 = ints;
* ----
*/
this(U, size_t n)(U[n] values)
if (n == 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, "="))
i.e. we can construct Tuple from array, but can't assign array to Tuple.
signature.asc
Description: PGP signature
