www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Alias of local variable's member

Well, I have a modest proposal.
While implementing a boilerplate code generation for event 
dispatching of the SDL using mixins and templates (btw having a 
great experience using subtying with alias-this), I tried to use 
a feature I was convinced to be there. Well, it wasn't but I 
found a different (superior) design circumventing the issue.
Still, it struck me as odd that the following doesn't work.

struct A {
     B b;
     alias b.safeMethod safeMethod; // here
     //alias b this; // alt 1
     //void safeMethod() { b.safeMethod(); } // alt 2

struct B {
     void safeMethod() {}
     void unsafeMethod() {}

void main() {
     A a;

The line marked with 'here' compiles but when I try to use that 
alias I get strange errors depending on the exact situation (eg 
as if tried to use a non-static in static context).
The first alternative ('alt 1') also exposes the unsafeMethod and 
introduces subtyping which is unwanted behavior.
The second alternative works but uses a pointless wrapper and 
isn't DRY. Moreover it becomes dramatically more complex once you 
have more parameters and go generic.

So my proposal is for the line marked with 'here' to 'just work'. 
One should be able to alias a variable's member. I think it is a 
useful feature. Its behavior is easily definable being quite 
similar to the accessing a member through alias-this just with 
more fine-grained control. I also don't see any problem from the 
code generation side.

I also think it would be useful with function variables too, 
given that you can already alias them. It just feels strange be 
able to use:

alias localVar name;

but not:

alias localVar.nonStatic name;

So what do you think? Do you see any problems with it or have any 
Aug 06 2013