www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Tuple/Typedef question

reply "Martin" <cjmpximu sharklasers.com> writes:
Is there a way to get Tuple (and Typedef) from the std.typecons
module to generate a new type that is unique on every
instantiation? What I mean is:

alias T1 = Tuple!(int, int);
alias T2 = Tuple!(int, int);

writeln(__traits(isSame, T1, T2)); // prints true

When using Typedef, the types are still the same:

alias T1New = Typedef!(T1);
alias T2New = Typedef!(T2);

writeln(__traits(isSame, T1New, T2New)); // still prints true

The documentation of Typedef says:
"Typedef allows the creation of a unique type which is based on
an existing type. Unlike the alias feature, Typedef ensures the
two types are not considered as equals."

Shouldn't the second part at least print false then?
Jan 11 2015
parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Sun, 11 Jan 2015 11:41:08 +0000
Martin via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:

 Is there a way to get Tuple (and Typedef) from the std.typecons
 module to generate a new type that is unique on every
 instantiation? What I mean is:
=20
 alias T1 =3D Tuple!(int, int);
 alias T2 =3D Tuple!(int, int);
=20
 writeln(__traits(isSame, T1, T2)); // prints true
=20
 When using Typedef, the types are still the same:
=20
 alias T1New =3D Typedef!(T1);
 alias T2New =3D Typedef!(T2);
=20
 writeln(__traits(isSame, T1New, T2New)); // still prints true
=20
 The documentation of Typedef says:
 "Typedef allows the creation of a unique type which is based on
 an existing type. Unlike the alias feature, Typedef ensures the
 two types are not considered as equals."
=20
 Shouldn't the second part at least print false then?
as for `Typedef!` -- you can use it's third arg, "cookie": import std.typecons; alias T1 =3D Tuple!(int, int); alias T2 =3D Tuple!(int, int); alias T1New =3D Typedef!(T1, T1.init, "t0"); alias T2New =3D Typedef!(T2, T2.init, "t1"); pragma(msg, __traits(isSame, T1New, T2New)); // false there was a heated discussion about `std.typecons.Typedef`, built-in `typedef` and other related things, but the decision was to keep the status quo.
Jan 11 2015
next sibling parent reply "Martin" <cjmpximu sharklasers.com> writes:
On Sunday, 11 January 2015 at 11:52:42 UTC, ketmar via 
Digitalmars-d-learn wrote:
 On Sun, 11 Jan 2015 11:41:08 +0000
 Martin via Digitalmars-d-learn 
 <digitalmars-d-learn puremagic.com>
 wrote:

 Is there a way to get Tuple (and Typedef) from the std.typecons
 module to generate a new type that is unique on every
 instantiation? What I mean is:
 
 alias T1 = Tuple!(int, int);
 alias T2 = Tuple!(int, int);
 
 writeln(__traits(isSame, T1, T2)); // prints true
 
 When using Typedef, the types are still the same:
 
 alias T1New = Typedef!(T1);
 alias T2New = Typedef!(T2);
 
 writeln(__traits(isSame, T1New, T2New)); // still prints true
 
 The documentation of Typedef says:
 "Typedef allows the creation of a unique type which is based on
 an existing type. Unlike the alias feature, Typedef ensures the
 two types are not considered as equals."
 
 Shouldn't the second part at least print false then?
as for `Typedef!` -- you can use it's third arg, "cookie": import std.typecons; alias T1 = Tuple!(int, int); alias T2 = Tuple!(int, int); alias T1New = Typedef!(T1, T1.init, "t0"); alias T2New = Typedef!(T2, T2.init, "t1"); pragma(msg, __traits(isSame, T1New, T2New)); // false there was a heated discussion about `std.typecons.Typedef`, built-in `typedef` and other related things, but the decision was to keep the status quo.
I can't believe I missed the cookie part. Thanks!
Jan 11 2015
parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Sun, 11 Jan 2015 12:00:19 +0000
Martin via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:

 as for `Typedef!` -- you can use it's third arg, "cookie":

   import std.typecons;

   alias T1 =3D Tuple!(int, int);
   alias T2 =3D Tuple!(int, int);

   alias T1New =3D Typedef!(T1, T1.init, "t0");
   alias T2New =3D Typedef!(T2, T2.init, "t1");

   pragma(msg, __traits(isSame, T1New, T2New)); // false

 there was a heated discussion about `std.typecons.Typedef`,=20
 built-in
 `typedef` and other related things, but the decision was to=20
 keep the
 status quo.
=20 I can't believe I missed the cookie part. Thanks!
this part deserves a better explanation in docs, 'cause it's easy to miss the details, especially for those who assumed that each `Typedef!` creates a distinct type. too bad that my writing skills sux (and i'm really biased against the current `Typedef!`).
Jan 11 2015
prev sibling parent "Dicebot" <public dicebot.lv> writes:
I use this Typedef implementation instead:

/// one with non-default initializer
template Typedef(T, istring name, T initval)
{
     static assert (name.length, "Can't create Typedef with an 
empty identifier");

             enum Typedef =
                 ("struct " ~ name ~
                 "{ " ~
                 T.stringof ~ " value = " ~ initval.stringof ~ ";" 
~
                 "alias value this;" ~
                 " }");
}

/// basic overload
template Typedef(T, istring name)
{
     static assert (name.length, "Can't create Typedef with an 
empty identifier");

             enum Typedef =
                 ("struct " ~ name ~
                 "{ " ~
                 T.stringof ~ " value; " ~
                 "alias value this;" ~
                 " }");
}

unittest
{
     mixin(Typedef!(int, "MyInt1"));
     mixin(Typedef!(int, "MyInt2"));

     static assert (!is(MyInt1 : MyInt2));
}
Jan 11 2015