| Thread overview | |||||||
|---|---|---|---|---|---|---|---|
|
December 21, 2015 Slicing AliasSeq-s | ||||
|---|---|---|---|---|
| ||||
http://dlang.org/spec/template.html#TemplateTupleParameter Apart from the obvious need for changing the references to tuples to alias sequences (for which I'm working on a PR), my question: Both the above page and http://dlang.org/phobos/std_meta.html refer to "slicing" alias sequences. In D slicing means just creating another reference to the same memory as the sliced object. Given that AliasSeq-s cannot be written to[*], it's not possible for me to test whether it's actually sliced or a new AliasSeq with the same elements is created. Otherwise I could do something like this: alias A = [int, 2, symbol]; alias B = A[1 .. $]; alias C = A[0 .. $ - 1]; A[1] = 3; // not possible static assert(B[0] == 3 && C[1] == 3); So out of curiosity I'd like to know how this is implemented in the compiler: as really a slice or a copy? (Posting this to D and not learn since it relates to compiler internals.) -- Shriramana Sharma, Penguin #395953 | ||||
December 21, 2015 Re: Slicing AliasSeq-s | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Shriramana Sharma | On Monday, 21 December 2015 at 09:06:08 UTC, Shriramana Sharma wrote:
> http://dlang.org/spec/template.html#TemplateTupleParameter
>
> Apart from the obvious need for changing the references to tuples to alias sequences (for which I'm working on a PR), my question:
>
> Both the above page and http://dlang.org/phobos/std_meta.html refer to "slicing" alias sequences. In D slicing means just creating another reference to the same memory as the sliced object.
>
> Given that AliasSeq-s cannot be written to[*], it's not possible for me to test whether it's actually sliced or a new AliasSeq with the same elements is created. Otherwise I could do something like this:
>
> alias A = [int, 2, symbol];
> alias B = A[1 .. $];
> alias C = A[0 .. $ - 1];
> A[1] = 3; // not possible
> static assert(B[0] == 3 && C[1] == 3);
>
> So out of curiosity I'd like to know how this is implemented in the compiler: as really a slice or a copy? (Posting this to D and not learn since it relates to compiler internals.)
I don't know the answer, but I suspect it's a (shallow) copy. Anyway, as you've noticed, there's no observable difference either way, so this is an implementation detail which the documentation shouldn't mention (if this was the intention behind your question).
| |||
December 21, 2015 Re: Slicing AliasSeq-s | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Shriramana Sharma | On Monday, 21 December 2015 at 09:06:08 UTC, Shriramana Sharma wrote: > Both the above page and http://dlang.org/phobos/std_meta.html refer to "slicing" alias sequences. In D slicing means just creating another reference to the same memory as the sliced object. AliasSeqs have no memory at runtime. They are a compile-time only construct. > So out of curiosity I'd like to know how this is implemented in the compiler: as really a slice or a copy? (Posting this to D and not learn since it relates to compiler internals.) Check the source! expression.d has class SliceExp. Look down to where it handles tuples (AliasSeq is the user-visible name for what the compiler internally calls a tuple). Slicing a tuple creates a new tuple that refers to the same objects as the previous one. So it doesn't deep copy... but remember this is irrelevant to any D program because an AliasSeq is just a compile-time list of constants anyway and thus cannot be modified and does not actually have a memory address in the generated program anyway. | |||
December 22, 2015 Re: Slicing AliasSeq-s | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | Adam D. Ruppe wrote: > Look down to > where it handles tuples (AliasSeq is the user-visible name for > what the compiler internally calls a tuple). Ouch. So even if the terminology gets abolished from Phobos, it's still lurking in the compiler? > Slicing a tuple creates a new tuple that refers to the same objects as the previous one. So it doesn't deep copy... but remember this is irrelevant to any D program I realize that but just wanted to know whether the word slicing is used in this context in the same sense as elsewhere. -- Shriramana Sharma, Penguin #395953 | |||
December 22, 2015 Re: Slicing AliasSeq-s | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Shriramana Sharma | On Tuesday, 22 December 2015 at 08:17:33 UTC, Shriramana Sharma wrote:
> Adam D. Ruppe wrote:
>> Slicing a tuple creates a new tuple that refers to the same objects as the previous one. So it doesn't deep copy... but remember this is irrelevant to any D program
>
> I realize that but just wanted to know whether the word slicing is used in this context in the same sense as elsewhere.
Well, from the perspective of the programmer, there's no semantic difference whether the compiler does a deep copy or does something like slice an array of aliases. Because there's no address to access, there's no perceivable difference between a shallow copy or a deep copy. So, slice is very much the right word to use in the documentation regardless of what the compiler is doing internally.
- Jonathan M Davis
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply