Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
November 07, 2020 How exactly does Tuple work? | ||||
---|---|---|---|---|
| ||||
I have a simple question. How exactly does Tuple work? In detail, I am interested in expand and opSlice. For expand, I have found the line: https://github.com/dlang/phobos/blob/master/std/typecons.d#L618 How does that work, where is the rest? What does it do? Similary I can access tuples with `[0]`. But there is no opSlice. It is also not specified in the docu: https://dlang.org/phobos/std_typecons.html#Tuple Last but not least, I can use `length`. How does that work? Where is it? I probably don't need an explenation, I mainly need to be pointed in the right direction. So instead of sacrificing your time writing an answer, a link to the lines I should look at would be fully suficient. Thanks in advance! BR, Jan |
November 07, 2020 Re: How exactly does Tuple work? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jan Hönig | On Saturday, 7 November 2020 at 18:02:26 UTC, Jan Hönig wrote: > I have a simple question. > How exactly does Tuple work? > > In detail, I am interested in expand and opSlice. A Tuple is a struct whose members are generated by type sequence instantiation: https://dlang.org/articles/ctarguments.html#type-seq-instantiation Indexing and slicing are implemented with `alias expand this`, which causes `t[i]` to be lowered to `t.expand[i]`. |
November 08, 2020 Re: How exactly does Tuple work? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | On Saturday, 7 November 2020 at 18:31:18 UTC, Paul Backus wrote:
> Indexing and slicing are implemented with `alias expand this`, which causes `t[i]` to be lowered to `t.expand[i]`.
Is there some recourse, which explains the `alias <something> this`? I still don't understand what it does. I can't imagine what it does to my my class/struct.
|
November 08, 2020 Re: How exactly does Tuple work? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jan Hönig | On Sunday, 8 November 2020 at 10:03:46 UTC, Jan Hönig wrote:
> Is there some recourse, which explains the `alias <something> this`?
If your object is used in a way that doesn't compile, the compiler will change `obj` to `obj.whatever_alias_this_is` and try again.
So say you have
struct S {
int a;
alias a this;
}
S obj;
obj += 5;
It will see that obj +=5 doesn't compile on its own, but it has alias a this so it changes `obj` to `obj.a` and tries again.
So `obj.a += 5;` is the end result.
|
November 08, 2020 Re: How exactly does Tuple work? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Sunday, 8 November 2020 at 13:10:33 UTC, Adam D. Ruppe wrote:
> On Sunday, 8 November 2020 at 10:03:46 UTC, Jan Hönig wrote:
>> Is there some recourse, which explains the `alias <something> this`?
>
> If your object is used in a way that doesn't compile, the compiler will change `obj` to `obj.whatever_alias_this_is` and try again.
>
> So say you have
>
> struct S {
> int a;
> alias a this;
> }
>
> S obj;
>
> obj += 5;
>
>
> It will see that obj +=5 doesn't compile on its own, but it has alias a this so it changes `obj` to `obj.a` and tries again.
>
> So `obj.a += 5;` is the end result.
So it's like inheritance resolved at compile time. It's inheritance with virtual member functions without overhead.
I am guessing only one alias works.
And we use this, because struct can't do inheritance and interface is abstract.
|
November 08, 2020 Re: How exactly does Tuple work? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jan Hönig | On Sunday, 8 November 2020 at 13:57:08 UTC, Jan Hönig wrote:
> So it's like inheritance resolved at compile time. It's inheritance with virtual member functions without overhead.
> I am guessing only one alias works.
>
> And we use this, because struct can't do inheritance and interface is abstract.
yeah, basically.
|
Copyright © 1999-2021 by the D Language Foundation