Proposal keyword 'outer'

Tom S h3r3tic at remove.mat.uni.torun.pl
Tue Jun 20 05:01:41 PDT 2006


Frank Benoit wrote:
> For inner classes and mixins, it would be good to have a reference to
> the next outer scope.
> 
> class C{
>  int i; // 1
>  class C1{
>   int i; // 2
>   void func( int a ){
>    i = a; // assign to 2
>    outer.i = a; // assign to 1
>   }
>  }
> }
> 
> In case of a mixin, outer is the scope of the instantiation.
> 
> outer can be chained like outer.outer.func() to go out in scope two levels.

I second that. Especially for mixins. In their current form, mixins are 
very error prone and using without caution begs for hard to spot errors.

One of the points of using mixins is for them to use the scope of their 
instantiation without any problems. But this is a huge problem itself. 
Let's say you write a module:

module a;

int someVar;

template MyMix() {
     void foo() { someVar = 1; }
}


if you then use that module like this:

module b;
import a;

mixin MyMix;

...
foo();

then everything is fine and the mixin uses the 'someVar' from 'a'. But 
if you use it like that:

module c;
private import a;

int someVar;
mixin MyMix;

...
foo();


then you may have some hard time figuring out what's going on. One might 
claim that in this case the programmer's assumptions are invalid, but 
then the current visibility rules in mixins are different from any other 
construct in D and thus yield themselves to mistakes. Assumptions are 
one thing and typos are another. The same situation might happen because 
of a typo and would probably be harder to track down than any other typo.

Another problem is that sometimes mixins should be able to use symbols 
from the modules they were defined in.

The 'outer' keyword suggestion would solve these problems in a 
consistent manner. Mixins would by default see the scope of their 
declaration and the 'outer' keyword would allow them to access the 
instantiation scope. No hidden problems, code 'injection' or 
inconsistencies.

++votes;


-- 
Tomasz Stachowiak  /+ a.k.a. h3r3tic +/



More information about the Digitalmars-d mailing list