www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Slicing AliasSeq-s

reply Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
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
Dec 21 2015
next sibling parent Marc =?UTF-8?B?U2Now7x0eg==?= <schuetzm gmx.net> writes:
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).
Dec 21 2015
prev sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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.
Dec 21 2015
parent reply Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
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
Dec 22 2015
parent Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Dec 22 2015