An Orthogonal Const Proposal

Oskar Linde oskar.lindeREM at OVEgmail.com
Fri Dec 7 11:33:02 PST 2007


Since everyone is doing it, the peer pressure got too heavy and I just 
felt I too had to make a const proposal ready to be shot down.  :)

Anyway, here is a very simple orthogonal const proposal that I believe 
catches most of the earlier suggestions in a more concise way.

Since the table below probably will become squished by the news reader, 
I made a small PDF:

http://www.csc.kth.se/~ol/const.pdf

Please prime and load your muskets. Enjoy :)

The same text as in the PDF follows:

== Definitions
*Values* are physical entities that occupy memory. A value can be either 
variable or constant. All values have a type. Examples of values in D are:
- basic types
- structs
- array slices (8 bytes on a 32 bit architecture)
- pointers (4 bytes on a 32 bit architecture)
- class references (4 bytes on a 32 bit architecture)

*References* are values that in some way refer to other values. 
References have access modes. In this proposal, for simplicity, there 
are only two different access modes: read-write and transitively read-only.

== Descriptions of earlier D const designs
In the first const iteration:
const      sets the read-only access mode
invariant  sets the read-only access mode and defines the
            /target value/ to be constant
final       defines the value to be constant

In the last const iteration:
const      sets the read-only access mode and defines the
            value to be constant
invariant  sets the read-only access mode and defines the
            value to be constant and defines the target
            value to be constant

== New suggested const design
const      defines the value to be constant
in         sets the read-only access mode (readonly would be
            an obvious alternative)

== Comparisons with earlier const iterations

first iteration         last iteration            new suggestion
const int *            TailConst!(int *) (?)     in int *
invariant int *        TailInvariant!(int *) (?) const(int) *
final invariant(int) *  invariant int *           const const(int) *
const Class            TailConst!(Class) (?)     in Class
final const Class       const Class (?)           const in Class
invariant(char)[]      invariant(char)[]         const(char)[]
final invariant(char)[] invariant char[]          const const(char)[]

-- 
Oskar



More information about the Digitalmars-d mailing list