any news on const/invariant?

Walter Bright newshound1 at digitalmars.com
Tue Nov 27 12:56:55 PST 2007


Janice Caron wrote:
> On 11/27/07, Walter Bright <newshound1 at digitalmars.com> wrote:
>> Since a const array cannot be implicitly cast to a non-const, he'll get
>> a compilation error.
> 
> Not necessarily. They might be relying on implicit cast to const in
> the first place. e.g.
> 
>     class A
>     {
>         const ubyte[] f()
>         {
>             ubyte[] b;
>             /* initialise b */
>             return b;
>         }
>     }
> 
> Here the programmer (who hasn't yet learned that "const ubyte[] f()"
> means "ubyte[] f() const" rather than "const(ubyte[]) f()") is
> assuming that when b is returned, it will be implicitly cast from
> ubyte[] to const ubyte[]. That assumption is wrong, but I believe the
> code still compiles.

Sure, but the code is also not broken.


>> It's the big feature of const-correctness, the code
>> won't compile if you get it wrong.
> 
> Of course, and that's why I love it.
> 
> But not all incorrect code involving const is necessarily const-incorrect. :-)
> 
> 
>>> Other possible syntaxes have been suggested. Here's another one to consider:
>>>     ReturnType const(functionName)(parameters...)
>> Sorry, but that's awful looking
> 
> I completely agree. Sorry to have mentioned it.



More information about the Digitalmars-d mailing list