| Thread overview | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 02, 2015 TypeTuple!(T...) vs Tuple!(T...) | ||||
|---|---|---|---|---|
| ||||
exactly what is the difference here? I have a rather large CTFE-generated TypeTuple in one of my structs in my project, and I can seemingly replace it with a Tuple with absolutely zero differences... except I compile about 60-70% slower. The tuple page is even confusing me http://dlang.org/tuple.html >A variable declared with a TypeTuple becomes an ExpressionTuple: >alias TL = Tuple!(int, long); is it using Tuple!(T...) and TypeTuple!(T...) interchangeably? | ||||
June 02, 2015 Re: TypeTuple!(T...) vs Tuple!(T...) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to rsw0x | On 06/02/2015 01:10 AM, rsw0x wrote: > exactly what is the difference here? > > I have a rather large CTFE-generated TypeTuple in one of my structs in > my project, and I can seemingly replace it with a Tuple with absolutely > zero differences... except I compile about 60-70% slower. > > The tuple page is even confusing me > http://dlang.org/tuple.html > >> A variable declared with a TypeTuple becomes an ExpressionTuple: >> alias TL = Tuple!(int, long); > > is it using Tuple!(T...) and TypeTuple!(T...) interchangeably? Tuple can hold only values, TypeTuple can hold types as well. Tuple can be created at run time, TypeTuple is a compile-time concept. My guess is that your TypeTuple consists only of values. That's why they work the same. I have two sections about TypeTuples here: http://ddili.org/ders/d.en/tuples.html#ix_tuples.TypeTuple,%20std.typetuple http://ddili.org/ders/d.en/templates_more.html#ix_templates_more.tuple%20template%20parameter Ali | |||
June 02, 2015 Re: TypeTuple!(T...) vs Tuple!(T...) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to rsw0x | On 06/02/2015 10:10 AM, rsw0x wrote: > exactly what is the difference here? > > I have a rather large CTFE-generated TypeTuple in one of my structs in > my project, and I can seemingly replace it with a Tuple with absolutely > zero differences... except I compile about 60-70% slower. > > The tuple page is even confusing me > http://dlang.org/tuple.html > >> A variable declared with a TypeTuple becomes an ExpressionTuple: >> alias TL = Tuple!(int, long); > > is it using Tuple!(T...) and TypeTuple!(T...) interchangeably? http://wiki.dlang.org/The_D_Programming_Language/Seq | |||
June 02, 2015 Re: TypeTuple!(T...) vs Tuple!(T...) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to rsw0x | On Tuesday, 2 June 2015 at 08:10:27 UTC, rsw0x wrote:
> The tuple page is even confusing me
> http://dlang.org/tuple.html
>
>>A variable declared with a TypeTuple becomes an ExpressionTuple:
>>alias TL = Tuple!(int, long);
>
> is it using Tuple!(T...) and TypeTuple!(T...) interchangeably?
Almost.
`Tuple` is defined at the top of the article as `template Tuple(E...) {alias Tuple = E;}`.
"TypeTuple" is later defined as a `Tuple` whose elements "elements are solely types".
So, in the context of the article, `Tuple!(int, long)` is a "TypeTuple", because it's a `Tuple` of types.
The article does not refer to `std.typecons.Tuple` or `std.typetuple.TypeTuple` at all. Specifically, the article's `Tuple` is not `std.typecons.Tuple`. And the article's "TypeTuple" is not `std.typetuple.TypeTuple`. However, going for bonus confusion points, the article's `Tuple` is defined the same as `std.typetuple.TypeTuple`.
| |||
June 02, 2015 Re: TypeTuple!(T...) vs Tuple!(T...) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Tuesday, 2 June 2015 at 09:34:28 UTC, Ali Çehreli wrote: > On 06/02/2015 01:10 AM, rsw0x wrote: [...] >> The tuple page is even confusing me >> http://dlang.org/tuple.html [...] > > Tuple can hold only values, TypeTuple can hold types as well. Tuple can be created at run time, TypeTuple is a compile-time concept. You're using the phobos names here, i.e. `std.typecons.Tuple` and `std.typetuple.TypeTuple`. The article doesn't. | |||
June 02, 2015 Re: TypeTuple!(T...) vs Tuple!(T...) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to rsw0x | On Tuesday, 2 June 2015 at 08:10:27 UTC, rsw0x wrote:
> exactly what is the difference here?
>
> I have a rather large CTFE-generated TypeTuple in one of my structs in my project, and I can seemingly replace it with a Tuple with absolutely zero differences... except I compile about 60-70% slower.
>
> The tuple page is even confusing me
> http://dlang.org/tuple.html
>
>>A variable declared with a TypeTuple becomes an ExpressionTuple:
>>alias TL = Tuple!(int, long);
>
> is it using Tuple!(T...) and TypeTuple!(T...) interchangeably?
Regular tuples are simply structs with a few methods like opIndex. Ex. `Tuple!(A,B,C)` is mostly equivalent to `struct { A _0; B _1; C _2; }`.
TypeTuples (whose name is IMO a misnomer) are special compile-time-only objects whose values are passed in the template parameters and can be types, aliases, or literal values. They aren't first-class types, and shouldn't be used as data storage, but they do have some unique properties that make them useful for metaprogramming.
Here's a small demonstration of TypeTuples:
import std.stdio;
import std.typetuple;
void foo(int x, int y, int z) {
writefln("x = %d, y = %d, z = %d", x, y, z);
}
void main() {
int a, b, c;
// Declare `vars` to be a typetuple containing aliases to a,b,c
alias vars = TypeTuple!(a, b, c);
vars[0] = 1; // Actually assign to a, b, c
vars[1] = 2;
vars[2] = 3;
foo(a,b,c);
foo(vars); // Same as above call
alias test_values = TypeTuple!(123, 456, 789);
foo(test_values); // Same as foo(123, 456, 7890)
// won't work; test_values[0] is a literal and can't be assigned to.
//test_values[0] = 321;
foreach(ref some_var; vars) {
// static foreach; this loop body is unrolled for each item in vars.
some_var += 5;
}
foo(vars);
}
Output:
$ rdmd ~/test.d
x = 1, y = 2, z = 3
x = 1, y = 2, z = 3
x = 123, y = 456, z = 789
x = 6, y = 7, z = 8
| |||
June 02, 2015 Re: TypeTuple!(T...) vs Tuple!(T...) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to anonymous | On Tuesday, 2 June 2015 at 11:49:18 UTC, anonymous wrote:
> On Tuesday, 2 June 2015 at 08:10:27 UTC, rsw0x wrote:
>> The tuple page is even confusing me
>> http://dlang.org/tuple.html
>>
>>>A variable declared with a TypeTuple becomes an ExpressionTuple:
>>>alias TL = Tuple!(int, long);
>>
>> is it using Tuple!(T...) and TypeTuple!(T...) interchangeably?
>
> Almost.
>
> `Tuple` is defined at the top of the article as `template Tuple(E...) {alias Tuple = E;}`.
>
> "TypeTuple" is later defined as a `Tuple` whose elements "elements are solely types".
>
> So, in the context of the article, `Tuple!(int, long)` is a "TypeTuple", because it's a `Tuple` of types.
>
> The article does not refer to `std.typecons.Tuple` or `std.typetuple.TypeTuple` at all. Specifically, the article's `Tuple` is not `std.typecons.Tuple`. And the article's "TypeTuple" is not `std.typetuple.TypeTuple`. However, going for bonus confusion points, the article's `Tuple` is defined the same as `std.typetuple.TypeTuple`.
That's... really confusing and probably needs a rewrite honestly.
| |||
June 02, 2015 Re: TypeTuple!(T...) vs Tuple!(T...) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to rsw0x | On Tuesday, 2 June 2015 at 18:48:16 UTC, rsw0x wrote:
> On Tuesday, 2 June 2015 at 11:49:18 UTC, anonymous wrote:
>> On Tuesday, 2 June 2015 at 08:10:27 UTC, rsw0x wrote:
>>> The tuple page is even confusing me
>>> http://dlang.org/tuple.html
>>>
>>>>A variable declared with a TypeTuple becomes an ExpressionTuple:
>>>>alias TL = Tuple!(int, long);
>>>
>>> is it using Tuple!(T...) and TypeTuple!(T...) interchangeably?
>>
>> Almost.
>>
>> `Tuple` is defined at the top of the article as `template Tuple(E...) {alias Tuple = E;}`.
>>
>> "TypeTuple" is later defined as a `Tuple` whose elements "elements are solely types".
>>
>> So, in the context of the article, `Tuple!(int, long)` is a "TypeTuple", because it's a `Tuple` of types.
>>
>> The article does not refer to `std.typecons.Tuple` or `std.typetuple.TypeTuple` at all. Specifically, the article's `Tuple` is not `std.typecons.Tuple`. And the article's "TypeTuple" is not `std.typetuple.TypeTuple`. However, going for bonus confusion points, the article's `Tuple` is defined the same as `std.typetuple.TypeTuple`.
>
> That's... really confusing and probably needs a rewrite honestly.
Yep.
Atila
| |||
June 02, 2015 Re: TypeTuple!(T...) vs Tuple!(T...) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Alex Parrill | On Tuesday, 2 June 2015 at 14:07:20 UTC, Alex Parrill wrote:
> On Tuesday, 2 June 2015 at 08:10:27 UTC, rsw0x wrote:
>> exactly what is the difference here?
>>
>> I have a rather large CTFE-generated TypeTuple in one of my structs in my project, and I can seemingly replace it with a Tuple with absolutely zero differences... except I compile about 60-70% slower.
>>
>> The tuple page is even confusing me
>> http://dlang.org/tuple.html
>>
>>>A variable declared with a TypeTuple becomes an ExpressionTuple:
>>>alias TL = Tuple!(int, long);
>>
>> is it using Tuple!(T...) and TypeTuple!(T...) interchangeably?
>
> Regular tuples are simply structs with a few methods like opIndex. Ex. `Tuple!(A,B,C)` is mostly equivalent to `struct { A _0; B _1; C _2; }`.
>
> TypeTuples (whose name is IMO a misnomer) are special compile-time-only objects whose values are passed in the template parameters and can be types, aliases, or literal values. They aren't first-class types, and shouldn't be used as data storage, but they do have some unique properties that make them useful for metaprogramming.
> ...
I'm using a typetuple for data storage, if I replace it with a Tuple as I said in the OP, my program compiles 60-70% slower on all three compilers. But it seems to be working just fine as is, which is why I'm confused. I'm going to read Ali's links and maybe get a better understanding here.
| |||
June 02, 2015 Re: TypeTuple!(T...) vs Tuple!(T...) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to rsw0x | I have PR for removing this obsolete article and replacing with more up to date one : https://github.com/D-Programming-Language/dlang.org/pull/986 | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply