www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Get identifier of "this"

reply "Andre" <andre s-e-a-p.de> writes:
Hi,

assuming I have following constuct:

public class Bank{
    public enum test()
    {
      return "writeln(\""~__traits(identfier, this)~"\");";
    }
}

public static void main(){
    Bank b = new Bank;
    mixin(b.test());
}

During compile time, following code should be generated:
writeln("b");

Is this possible? For the demo coding I receive the error, that
"this"
has no identifier.

Kind regards
Andre
Sep 17 2012
next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 9/17/12, Andre <andre s-e-a-p.de> wrote:
 Get identifier of "this"
You can't really get that info at runtime, a class object isn't bound to a name, 'this' has no identifier. Symbols (like variables) have identifiers, not objects.
 public class Bank{
Unnecessary, declarations are public by default.
     public enum test()
'enum' has no meaning in a return type. Either it's 'auto' which infers the return type from the function body, or it's a specific type like 'string' (or void if no return type).
     {
       return "writeln(\""~__traits(identfier, this)~"\");";
typo: identifier, not identfier
     }
 }
 public static void main(){
public and static have no meaning here.
 During compile time, following code should be generated:
 writeln("b");
It's not possible to mixin a string at compile-time from a string returned from a method of an object which is instantiated at runtime. If you want the identifier of the variable then you don't have to deal with the 'this' reference at all, e.g.: property string test(alias symb)() { return "writeln(\"" ~ __traits(identifier, symb) ~ "\");"; } class Bank { } void main() { Bank b = new Bank; mixin(test!b); } Your original sample does cause a compiler ICE but I don't know if it's worth filing since the code was invalid.
Sep 17 2012
next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 17 Sep 2012 14:01:35 -0400, Andrej Mitrovic  
<andrej.mitrovich gmail.com> wrote:

 Your original sample does cause a compiler ICE but I don't know if
 it's worth filing since the code was invalid.
Please file, ICE should never occur. -Steve
Sep 17 2012
parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 9/17/12, Steven Schveighoffer <schveiguy yahoo.com> wrote:
 Please file, ICE should never occur.
You're bound to find a small million of these when it comes to typos in templates. :) http://d.puremagic.com/issues/show_bug.cgi?id=8679
Sep 17 2012
prev sibling parent "Andre" <andre s-e-a-p.de> writes:
On Monday, 17 September 2012 at 18:01:02 UTC, Andrej Mitrovic
wrote:
 On 9/17/12, Andre <andre s-e-a-p.de> wrote:
 Get identifier of "this"
You can't really get that info at runtime, a class object isn't bound to a name, 'this' has no identifier. Symbols (like variables) have identifiers, not objects.
... Hi Andrej, thanks a lot for the working sample coding, this helps me. Kind regards Andre
Sep 17 2012
prev sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Monday, September 17, 2012 19:43:24 Andre wrote:
 Hi,
 
 assuming I have following constuct:
 
 public class Bank{
 public enum test()
 {
 return "writeln(\""~__traits(identfier, this)~"\");";
 }
 }
 
 public static void main(){
 Bank b = new Bank;
 mixin(b.test());
 }
 
 During compile time, following code should be generated:
 writeln("b");
 
 Is this possible? For the demo coding I receive the error, that
 "this"
 has no identifier.
No. It's not possible. __traits is a compile-time construct, and Bank's definition knows nothing about any variables of type Bank declared elsewhere. Heck, the code for Bank could be compiled months before the code with b in it is even written (and then has the code with Bank linked to it when it's compiled). So no, it can't know about b, and what you're trying to do won't work. - Jonathan M Davis
Sep 17 2012