any news on const/invariant?

Jarrett Billingsley kb3ctd2 at yahoo.com
Wed Nov 28 06:20:09 PST 2007


"Janice Caron" <caron800 at googlemail.com> wrote in message 
news:mailman.147.1196235159.2338.digitalmars-d at puremagic.com...
> 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". :-)
>

Read the conference slides?  In fact you even guessed the keyword --  
"return".  I think it's something like:

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

The return type is then dependent upon the constness of the input.  It's 
_kind of_ like a templated function, but unlike a templated function it can 
be virtual. 





More information about the Digitalmars-d mailing list