Read-only array reference

Hasan Aljudy hasan.aljudy at gmail.com
Sat Jul 8 12:43:31 PDT 2006



Sean Kelly wrote:
> kris wrote:
> 
>> Hasan Aljudy wrote:
>> [snip]
>>
>>> Then how about introducing a new type: read-only array reference.
>>> (The idea is inspired by Java's immutable string class)
>>>
>>> It would be a distinct type, but built into the language core.
>>> It has a different declaration syntax from regular arrays.
>>> It cannot be casted to anything at all, absolutly no pointer 
>>> manipulation is allowed either.
>>> The content of the array cannot be modified (further explanation below).
>>> The reference itself *can* be modified to refer to another array, but 
>>> it would still be a read-only reference.
>>> Any type of regular array reference (dynamic/static/associative) can 
>>> be converted to a read-only array reference.
>>
>>
>> Having a true set of readonly array[] types would certainly resolve 
>> all such issues that I've personally encountered with D, although some 
>> might balk at having another set of TypeInfo? On the other hand, that 
>> may be wholly preferable to implementing a type-modifier instead?
>>
>> It is also fully backward compatible ;)
>>
>> [snip other good stuff]
>>
>> Really don't think any special-cases are required here ~ if a readonly 
>> array, of any type, is used as an lValue in any manner, it is a 
>> compile-time error. Simple, neat, easy to understand.
> 
> 
> A ReadOnly array would still need to access the contents of a standard 
> array for this to be useful, so it couldn't be completely excluded from 
> evaluation as an lvalue--initializations would be exempt.
> 
> 
> Sean

It's actually an array "reference" .. you can make it refer to (point 
to) any normal array, i.e. you can change what it refers to.
You just can't use that reference to change the properties or contents 
of the array.
You can use it to read the contents of the array it refers to.

Think of it as an interface with read-only methods and no write methods.



More information about the Digitalmars-d-learn mailing list