Tuple opAssign type deduction

ketmar via Digitalmars-d digitalmars-d at puremagic.com
Tue Dec 23 16:44:34 PST 2014


On Wed, 24 Dec 2014 00:16:33 +0000
aldanor via Digitalmars-d <digitalmars-d at puremagic.com> 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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20141224/ff1549ce/attachment.sig>


More information about the Digitalmars-d mailing list