any news on const/invariant?

Janice Caron caron800 at googlemail.com
Fri Nov 30 01:18:48 PST 2007


On Nov 29, 2007 8:25 PM, Walter Bright <newshound1 at digitalmars.com> wrote:
> const as storage class: Const local variables can be initialized with
> const types, so there is definitely a place for them:
>
> void foo(const int *p)
> {
>      const q = p;
> }
>
> q does *not* point to an invariant.

Actually is does. If you followed all the details of my previous post,
you should recall that I said: "with the caveat that when
const-as-storage-class is applied to types which require storage, it
actually means invariant". Thus, with that caveat borne in mind, the
above code actually says:

    void foo(invarant int * p)
    {
        invariant q  = p;
    }

I think you applied my caveat selectively! :-)

However, I grant you that that is confusing, and I would be opposed to
anything which is confusing, so clearly it won't fly. Unless ... we
add one further rule...

(*) disallow altogether both "const" and "invariant" as function
parameter storage classes.

This would force the writer of the function to declare

    void foo(const(int)* p)

or

   void foo(const(int *) p)

(although the latter is pointless since we don't really care about
head-constness), instead of

   void foo(const int * p)

and either way, the body of the function could then become

    auto q = p;

so we'd end up with

    void foo(const(int)* p)
    {
        auto q = p;
    }

which seems eminently readable to me.



More information about the Digitalmars-d mailing list