const member function synatx?

Janice Caron caron800 at googlemail.com
Fri Feb 15 22:57:03 PST 2008


On 15/02/2008, im <no at body.com> wrote:
>  I think this is really confusing: is 'const' trying to specify the
>  'ReturnType' or the memberFunc?
>
>  And if I want to specify both, then I have to write:
>
>  const const ReturnType  memberFunc(param) {}, or
>  const const(ReturnType) memberFunc(param) {}

Actually, only

    const const(ReturnType) memberFunc(param) {}

would work.

This has been mentioned before, and for what it's worth, I agree with
you. Just some comments though. Walter likes the existing syntax,
because it means you can do:

    const
    {
        /* several functions */
    }

It is unusual ever to need to return a const(ReturnType), since why
would you want to prevent the callee from modifying their copy of the
return value? That said, it is very common to want to return a
const(T)[] for some T, so the problem doesn't go away.

const-at-the-end syntax does work. I don't know if this is a permanent
feature or not, but currently you can say:

    const(ReturnType) memberFunc(param) const {}

And finally, I have argued in the past (and still believe) that the D
syntax should be

    const(this) const(ReturnType) memberFunc(param) {}

For an extra six characters, you get complete clarity. const(this)
would mean that the symbol "this" will (transitively) not be modified
within its scope. Walter's desire to encompass multiple functions
would thus be preserved. i.e.

    const(this)
    {
        /* several functions */
    }

and in addition, the new syntax lends itself to future expansion (such
as const(outer), etc.). Unfortunately, last I heard, Walter wasn't too
keen on this suggestion, possibly because it means typing six more
characters.

But either way, I definitely believe that

    const ReturnType memberFunc(param) {}

/must/ have exactly one interpretation, and that interpretation should be

    const(ReturnType) memberFunc(param) {}

and not, as is currently the case

    ReturnType memberFunc(param) const {}

I agree that that /is/ confusing, and is one of those little creases
in the new const system that we still need to iron out.



More information about the Digitalmars-d mailing list