www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Relative Aliases?

reply Jonathan Marler <johnnymarler gmail.com> writes:
Is there a way to get an alias to a symbol relative to the 
current location?  I'm looking for a general solution but I'll 
show an example to demonstrate one use case.

Say we want to access the alias to the current function symbol.  
Obviously we can use the name of the current function, i.e.

     void foo()
     {
         alias TheAliasWeWant = foo;
     }

but this means the code must be modified for every function, i.e.

     void foo1()
     {
         alias TheAliasWeWant = foo1;
     }
     void foo2()
     {
         alias TheAliasWeWant = foo1; // uh oh, should be foo2, 
copy paste error
     }

is there a way to implement a library function/template that can 
do this?

     void foo1()
     {
         alias TheAliasWeWant = currentFunctionAlias();
     }
     void foo2()
     {
         alias TheAliasWeWant = currentFunctionAlias(); // can't 
make copy/paste error
     }

Note that this is similar to the special keywords like 
__FUNCTION__, __MODULE__, the difference being that we want a 
symbol instead of a string.  We may be able to create an 
interesting solution to the function case where we unmangle the 
__FUNCTION__ result and then mixin the resulting string, but I'm 
also looking for the ability to get an alias to the current type, 
i.e.

struct Foo
{
     void foo()
     {
         alias TheAliasWeWant = Foo;
         alias TheAliasWeWant = currentTypeAlias(); // possible?
     }
}
May 28
next sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 28.05.2017 17:04, Jonathan Marler wrote:
 Is there a way to get an alias to a symbol relative to the current 
 location?  I'm looking for a general solution but I'll show an example 
 to demonstrate one use case.
 
 Say we want to access the alias to the current function symbol. 
 ...
__traits(parent,{})
May 28
parent Jonathan Marler <johnnymarler gmail.com> writes:
On Sunday, 28 May 2017 at 15:07:55 UTC, Timon Gehr wrote:
 On 28.05.2017 17:04, Jonathan Marler wrote:
 Is there a way to get an alias to a symbol relative to the 
 current location?  I'm looking for a general solution but I'll 
 show an example to demonstrate one use case.
 
 Say we want to access the alias to the current function 
 symbol. ...
__traits(parent,{})
THANK YOU!!
May 28
prev sibling parent tsbockman <thomas.bockman gmail.com> writes:
On Sunday, 28 May 2017 at 15:04:15 UTC, Jonathan Marler wrote:
 I'm also looking for the ability to get an alias to the current 
 type, i.e.

 struct Foo
 {
     void foo()
     {
         alias TheAliasWeWant = Foo;
         alias TheAliasWeWant = currentTypeAlias(); // possible?
     }
 }
For types, you can use `typeof(this)` (or `typeof(cast() this)` if you want to strip type qualifiers).
May 28