Inability to dup/~ for const arrays of class objects

Maxim Fomin maxim at maxim-fomin.ru
Thu May 30 00:01:06 PDT 2013


On Thursday, 30 May 2013 at 06:08:09 UTC, Diggory wrote:
> On Thursday, 30 May 2013 at 05:54:57 UTC, Maxim Fomin wrote:
>> On Thursday, 30 May 2013 at 05:44:43 UTC, Diggory wrote:
>>> On Thursday, 30 May 2013 at 05:41:06 UTC, Maxim Fomin wrote:
>>>> On Wednesday, 29 May 2013 at 23:45:04 UTC, Ali Çehreli wrote:
>>>>> On 05/29/2013 03:59 PM, Peter Williams wrote:
>>>>>
>>>>> > I've been trying to find out how non ref array arguments 
>>>>> > are
>>>>> passed to
>>>>> > functions in D but can't find any documentation on it.
>>>>>
>>>>> The following concepts are relevant:
>>>>>
>>>>> - Dynamic array: Maintained by the D runtime
>>>>
>>>> Generally yes, but not always http://dpaste.dzfl.pl/ffbcb449
>>>
>>> That's not a dynamic array, it's a slice of a static array.
>>
>> And this is a problem, because article about D slices 
>> encourages to call some raw memory (which almost never is 
>> directly manipulated and doesn't appear in source code) as a 
>> dynamic array, and dynamic array as a slice.
>
> The article about slices on this site - 
> http://dlang.org/d-array-article.html is perfectly correct.

No, the article is incorrect in vocabulary because it contradicts 
to the spec definitions, and spec incompletence is not a reason 
to dilute what is already defined. What actually happens is:
1) T[] is defined as dynamic array,  PostfixExpression [ ] is 
defined as SliceExpression.
2) Array article calls dynamic arrays as slices. Since notion of 
dynamic array is occupied, the article calls GC memory as dynamic 
array. This is a complete contradiction to spec.
3) Folks start using these misleading definitions.

This actually leads to incorrect assumptions, as was pointed out 
previously that dynamic array points only to runtime memory 
(which is not always the case).

>>
>>>>
>>>>> - Fixed-length array (aka static array): Can be on the stack
>>>>>
>>>>> - Slice: An efficient tool to access a range of elements 
>>>>> (of any type of array)
>>>>>
>>>>> Usually, it is the slice that gets passed:
>>>>>
>>>>> void foo(int[] slice);
>>>>
>>>> Isn't it a dynamic array? I don't understand listing slice 
>>>> as separate type of arrays or mixing meaning of slice and 
>>>> dynamic array. As far as D spec is concerned, slice is a 
>>>> SliceExpression which produces dynamic array for array types.
>>>
>>> You can't directly access dynamic arrays in D, you can only 
>>> manipulate views of them using slices. "new int[5]" creates a 
>>> new dynamic array internally but only returns a slice of that 
>>> array.
>>
>> That's clear, issue here is misleading definitions used by D 
>> slices article.
>>
>> typeof(slice) => int[]
>>
>> From array page spec: type[] => dynamic array. So, int[] slice 
>> is a parameter of type 'dynamic array'.
>
> Yep, it's misleading but the D documentation is misleading and 
> wrong in a lot of places and I'd say here the problems are 
> relatively minor. Still they should be fixed.

This is not a reason to confuse what exists.

>>
>> From expression spec page what slice is:
>> SliceExpression:
>>   PostfixExpression [ ]
>>   PostfixExpression [ AssignExpression .. AssignExpression ]
>>
>> and clearly 'slice' object is not an expression.
>
> This is talking about the slice operator, not slices. The slice 
> operator can be applied to any object which defines it.

than please point out what else slice according to spec is (spec 
mentions slices in context of SliceExpressions and it is the only 
valid usage).


More information about the Digitalmars-d mailing list