question on tuples and expressions

Jarrett Billingsley jarrett.billingsley at gmail.com
Mon Jan 5 08:20:52 PST 2009


On Mon, Jan 5, 2009 at 6:33 AM, leo <leo at clw-online.de> wrote:
> Hi,
> while I was reading the language specification I stumbled across the
> following problem:
>
> Tuple!(int, int) x = Tuple!(1, 2);  // doesn't compile
> Tuple!(int, int) y = (1, 2);    // leads to y being (2, 2)
>
> How can I initialize a tuple by providing another tuple?
>
> I know it's still possible to assign each element of the tuple separately,
> I'm just wondering what sense it makes
> to define an expression (1, 2) to be 2 of type int rather than (1, 2) of
> type (int, int).
> Is there any case in which this would provide an advantage?

It seems that the compiler isn't quite smart enough to understand an
(int, int) initializer for an (int, int) variable.  You can, however,
let the compiler figure out the type of the variable from the
initializer:

auto x = Tuple!(1, 2);
pragma(msg, typeof(x).stringof); // prints (int, int)

Furthermore, assigning a tuple in a normal assignment works:

x = Tuple!(6, 7); // fine


More information about the Digitalmars-d-learn mailing list