Arrays passed by almost reference?

Travis Boucher boucher.travis at gmail.com
Thu Nov 5 22:26:21 PST 2009


Yigal Chripun wrote:
> On 06/11/2009 07:07, Bob Jones wrote:
>> "Leandro Lucarella"<llucax at gmail.com>  wrote in message
>> news:20091106035612.GI3748 at llucax.com.ar...
>>>>
>>>> I am not fully against pass-by-ref arrays, I just think in passing by
>>>> reference all of the time could have some performance implications.
>>>
>>> OK, make 2 different types then: slices (value types, can't append, they
>>> are only a view on other's data) and dynamic arrays (reference type, can
>>> append, but a little slower to manipulate).
>>>
>>> It's a shame this idea didn't came true after all...
>>
>> Thats the whole problem. Dynamic arrays and slices are not the same 
>> thing,
>> and having a syntax that allows code to be ignorant of which it is 
>> dealing
>> with is always going to have problems imo. Being able to resize or 
>> append to
>> slices is fubar imo.
>>
>> I'd go with slices being value types, no concentenation, or resizing /
>> reallocating, etc..
>>
>> Dynamic arrays could be a library type. A templated struct that has a
>> pointer, length, or whatever. They can have operator overloads for 
>> implicit
>> convertion to slices, so any code that accepts slice can take dynamic
>> arrays, and prevent side effects. Code that is going to reallocate, 
>> has to
>> take a dynamic array. So at least whats happening is more 
>> obvious/explicit.
>>
>>
> 
> I agree with the above.
> 
> the semantics should be:
> DynamicArray!(T) as a dynamic array
> int[x] is a static array
> RandomAccessRange!(T) is a slice
> 
> int[] a; // compile error
> 
> (names are not important ATM)
> 
> I don't think there's a need for a dedicated array slice type and 
> instead they should be range types.
> It should be easy to change underlining containers with compatible range 
> types.
> 

You can create DynamicArray and RandomAccessRange already now.

Currently int[] a is very intuitive in its purpose, its just some of the 
implementation details that get confusing.

int doSomething(in int[]) a)
tells me doSomething is going to process an  int array of any size and 
not modify it.

int doSomething(int[] a)
tells me doSomething is going to process an int array of any size and 
possibly modify it.

An explicit 'out int[] a' would make it even more obvious what the 
function is going to do.

The thing is, dynamic arrays and slices are pretty much the same thing, 
its just hard to track what the underlying store points to.



More information about the Digitalmars-d mailing list