Latest const expansion
Yigal Chripun
yigal100 at gmail.com
Tue Sep 11 11:03:36 PDT 2007
Regan Heath wrote:
> I'm interested in what people think of the idea(s) I had for expanding
> on Walter's latest const proposal and handling head/tail const on class
> references.
>
> Ideas originally posted as replies, here:
>
> http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=58107
>
> http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=58112
>
>
> Also here:
>
> http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=58114
>
> http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=58115
>
>
>
> In short, Where:
>
> head const - means the class reference itself cannot be modified
> tail const - means the class members cannot be modified
>
> Where T is a class:
>
> const(T) indicates both head/tail const
> const(*T) indicates tail const
> const(&T) indicates head const
>
> Eg.
>
> class A { int a; }
>
> const(A) a; //a cannot be modified, a.a cannot be modified
> const(*A) b; //a can be modified, a.a cannot be modified
> const(&A) c; //a cannot be modified, a.a can be modified
>
> The idea being that const(*T) means const("value of the type T") where
> the value of a class reference is the class instance itself. Likewise
> const(&T) means const("reference/pointer of the type T") indicating the
> reference is to be const.
>
> For template/meta-programming purposes the syntax "const(*T)" and
> "const(&T)" should be legal for T where T is a value type, i.e. struct,
> union, and primitives (excluding pointers) but will be identical in
> behaviour to "const(T)"
>
>
> For pointers and arrays the "const(*T)" (tail const) syntax is not
> required as we can say:
>
> const(T)[]
> const(T)*
>
> but perhaps for meta-programming purposes it should be legal, eg.
>
> const(*char[])
> const(*char*)
>
>
> For pointers and arrays the "const(&T)" (head const) syntax is required
> as there is currently no way to get a const array reference to mutable
> data, so:
>
> const(&char[])
> const(&char*)
>
> would specify a const array reference and const pointer respecitvely
> both to mutable 'char' data.
>
> Thoughts? Too complicated? :P
>
> Regan
Hello,
I'm a new user of D.
I've read about the const system and i agree with everyone who wants a
head/tail const. however i disagree with the sytax proposed by various
people as being unreadable.
const(*char*) or const (&char**) is quite confusing in my opinion.
my proposal is to simply use the terms tail/head, so the syntax will be
[head|tail] const (T) | const T
for example:
head const (C) c; // c is const but c.x isn't
tail const (C) c; // c is mutable but c.x is const
const (C) c; // as before, both c and c.x are const
head const C c; // Error
tail const C c; // Error
Yigal Chripun
More information about the Digitalmars-d
mailing list