'in' storage class

Robert Fraser fraserofthenight at gmail.com
Wed Sep 5 14:13:07 PDT 2007


Funog Wrote:

> Nathan Reed Wrote:
> 
> > Funog wrote:
> > > void testFinal(final int[] foo)
> > > {
> > >     foo[0] = 10;        //OK
> > >     foo = new int[20];  //ERROR
> > > }
> > > void testConst(const int[] foo)
> > > {
> > >     foo[0] = 10;        //ERROR
> > >     foo = new int[20];  //ERROR
> > > }
> > > void testFinalConst(final const int[] foo)
> > > {
> > >     foo[0] = 10;        //ERROR
> > >     foo = new int[20];  //ERROR
> > > }
> > > 
> > > 
> > > So what is the difference between 'const' and 'final const' ?
> > > 
> > 
> > I suppose there's no practical difference in that case since you're 
> > using the transitive const.  Note that final const(int)[] foo is 
> > different from const(int)[] foo though.
> > 
> > Thanks,
> > Nathan Reed
> 
> 
> 
> I agree... But then, what is the point of having 'in' equivalent to 'final const scope' rather than just 'const scope' ?
> 

The difference is easier for me to grasp when talking about class references than arrays.

class Foo { int bar; }

final Foo baz;
baz.bar = 5; // Okay
baz = quux; // Illegal

const Foo quux; // Equivilent, I _think_ to const(Foo) quux;
quux.bar = 5; // Illegal
quux = baz; //Okay

In other words, for a class reference, head-const (final) means that the reference can't be changed to point to something else, but the data can be changed. Head-const is not transitive. Tail-const (const or invariant, in the case of a class reference) means that the thing being pointed to cannot be changed, but the reference can be.

This is generalized to array references and pointers.


More information about the Digitalmars-d-learn mailing list