const, final, scope function parameters

Myron Alexander someone at somewhere.com
Mon May 28 04:57:14 PDT 2007


Frank Benoit wrote:
> 
> foreach( m; myCollection ){
>   C c = m.getC();
>   c.doA();
>   c.doB();
>   C c2 = m.getTheOtherC();
> }
> 

I think that, as defined before, immutable would mean that the doA, doB 
call would fail unless they do not affect state as well. What you seem 
to be talking about is "final" which is that the reference is immutable, 
but the instance is not.

If I am right, then your example would have to be rewritten as such:

foreach( m; myCollection ){
   var C c = m.getC();
   c.doA();
   c.doB();
   var C c2 = m.getTheOtherC();
}

but then you could update the value of "c" to point to another 
reference. Thus the semantic is too course. We are back to having const, 
final, invariant.

In Walter's proposal:

foreach( m; myCollection ){
   final C c = m.getC();
   c.doA();
   c.doB();
   final C c2 = m.getTheOtherC();
}

would do what you want. So are you suggesting that final be the default 
storage attribute?

Regards,

Myron.



More information about the Digitalmars-d mailing list