Inner classes - More expressiveness needed

Nathan Reed nathaniel.reed at gmail.com
Thu Oct 25 09:21:44 PDT 2007


Janice Caron wrote:
> I can't call this a bug, because I think it's behaving as documented.
> Nonetheless, it seems to be that D could be more expressive. The
> following won't compile:
> 
>     class Outer
>     {
>         int n;
> 
>         class Inner
>         {
>             static int f() { return n; }
>         }
>     }
> 
> The error is:
> Error: need 'this' to access member n
> 
> The problem is that the word "static" is horribly overused, and
> doubles up to mean BOTH "the function does not have a this pointer"
> AND "this function does not have an outer pointer".
> 
> There seems no way to express "please can my function have an outer
> pointer but not a this pointer" (which is what I was trying to
> achieve).
> 
> There also seems no way to express "please can my function have a this
> pointer but not an outer pointer". (I wasn't trying to achieve that,
> but it could conceivably be useful).
> 
> I wonder if we might find some way of expressing those ideas?

Isn't what you want just a member function of Outer?

If you need to get at it through an object of type Inner, you could 
simply use a forwarding function, e.g.:

class Outer {
     int n;
     int f() { return n; }
     class Inner {
         int f() { return outer.f(); }
     }
}

Thanks,
Nathan Reed



More information about the Digitalmars-d mailing list