any news on const/invariant?

Janice Caron caron800 at googlemail.com
Tue Nov 27 23:32:23 PST 2007


On Nov 27, 2007 9:42 PM, Bill Baxter <dnewsgroup at billbaxter.com> wrote:
> The real annoyance with C++ const is the redundant methods in both const
> and non-const flavors.

Make that THREE flavours in D

    byte[] f(byte[] b) {/*...*/}
    const(byte)[] f(const(byte)[] b) {/*...*/}
    invariant(byte)[] f(invariant(byte)[] b) {/*...*/}

>  That's the thing to target for elimination.

Absolutely agreed! My favourite proposed syntax for that is

    constType(byte)[] f(constType(byte)[] b) {/*...*/}

"constType" is a new keyword. The idea is that the compiler generates
at most three versions of the function, with "constType" replaced by
"const", "invariant" or nothing at all in each case. The symbol
"constType" would be available inside the body of the function too. If
any of the three flavours don't compile, then it is not an error -
instead, they are simply not instantiated.

You don't have to invent a new keyword, of course. Any symbol will do
instead of "constType", as long as it's clear what's going on, and not
ambiguous - even "return". :-)



More information about the Digitalmars-d mailing list