Read-only array reference

kris foo at bar.com
Fri Jul 7 20:52:36 PDT 2006


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.

What's missing?

* interfacing with extern(C) functions via pointers?

* pointers in general, within D?

I suspect the above could be handled in a minimalist manner? e.g. I 
suspect we could probably get by without readonly-pointers in D. I use 
pointers quite a bit as function arguments, but I can't think of a case 
where an array wouldn't suffice instead (in order to get immutable 
arguments).

In other words, to simplify things as much as possible we should 
consider the minimal subset with the most mileage ~ the best bang for 
the buck

* what else?





More information about the Digitalmars-d-learn mailing list