any news on const/invariant?

Brad Roberts braddr at puremagic.com
Tue Nov 27 16:22:49 PST 2007


On Tue, 27 Nov 2007, Walter Bright wrote:

> 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.
> > :-)

Also, don't forget the array vs slice changes that have been proposed in 
the past (page 26 of the conference presentation).  That set of changes 
will help with this class of problem in a way that const can't.

Later,
Brad



More information about the Digitalmars-d mailing list