Read-only array reference
Sean Kelly
sean at f4.ca
Sat Jul 8 09:17:20 PDT 2006
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
More information about the Digitalmars-d-learn
mailing list