const

Walter Bright newshound1 at digitalmars.com
Fri Mar 28 15:27:13 PDT 2008


Russell Lewis wrote:
> What I've been thinking of late is that const is a multidimensional 
> concept.  Different types of const may have different answers to each of 
> the following questions:
> 
>     * Who declares that this entity is "const"?
>         - Compiler?
>         - Library writer?
>         - Ordinary coder?
>     * Who is limited by it?
>         - Compiler ("don't optimize")?
>         - Caller?
>         - Callee?
>     * Who enforces it?
>         - Compiler?
>         - Programmer?
>         - A combination?
>     * Who is interested in it?
>         - Compiler?
>         - Programmer?
>         - Other modules?
>         - Maintainer trying to understand the code?
>     * Who is entitled to make assumptions based on it?
>         - Compiler optimizations?
>         - Programmer assert()s and algorithms?
>         - Other modules?
>     * How long does it last?
>         - Forever?
>         - From here on out?
>         - In this block?
>         - So long as this pointer is valid?
>         - Except when we call another function?
>         - Almost always, except for certain windows?
>     * What are we protecting?
>         - Logical semantics?
>         - Function return values?
>         - Binary storage?
>     * What if we want to modify this?
>         - Perform COW?
>         - Use thunks? (COW with shared result pointer)
>         - Never modify it?
> 
> So "const" is (at least) a 8-dimensional concept.  When I talk a 
> mathematical understanding of const, I mean that somebody needs to list 
> out the various answers to the questions above (there probably are more 
> than I listed) and formally examine each one.  Also, find more 
> questions.  (When I first composed this email, I had 6 questions.  Just 
> in the last 10 minutes, the list expanded to 8 questions! )
> 
> What I'm hoping for is a system that either:
> A) Can express any arbitrary point in the above space,
>     or
> B) Makes convincing arguments why some of those points are unnecessary.

I think all these have been answered, although not all in one place.


> Overkill?  Perhaps.  But we've tried repeatedly to make const work, and 
> what we've learned is that "const is subtle, and hard to get right." 
> More to the point, as we move into uncharted territory (such as trying 
> to use "pure" functions and such for efficient multithreading), I am 
> worried that some of the points in the space above (which seemed 
> meaningless to our imperative-programming-trained brains) are going to 
> become suddenly important.  What do we do, then?

We know what we want to do with const, it was just hard getting it 
worked in correctly to the existing language semantics.



More information about the Digitalmars-d mailing list