readonly storage class

Piotr Szturmaj bncrbme at jadamspam.pl
Sun Apr 8 03:58:40 PDT 2012


Benjamin Thaut wrote:
> The only thing the function cares about is, that it will not change the
> data passed to it. It would be kind of nice to have a thrid storage
> class "readonly".

Const is really a "readonly" view of data. I think that immutable should 
be named const, and const should be named readonly, so they won't cause 
confusion.

If you need a function that don't change data just mark parameters as 
const. All mutable, const and immutable types are implicitly convertible 
to const.

> It can not be casted back to mutable and it can not be
> implicitly shared among threads, but both const and immutable implicitly
> convert to readonly, because both of these storage classes lose one of
> their properties during conversion. That way you only have to write the
> function once and can pass both const and immutable data to it.

Yes, this is how const (as "readonly") works. If you think about 
readonly, use const. The only drawback are the names.



More information about the Digitalmars-d mailing list