www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Why this UDP will not compile?

reply d coder via Digitalmars-d <digitalmars-d puremagic.com> writes:
DMD wont compile this. I get an error saying:
/tmp/test.d(3): Error: need 'this' for 'foo' of type 'int'
Failed: ["dmd", "-v", "-o-", "/tmp/test.d", "-I/tmp"]

Is this a bug or is it illegal code?

// Regards
// Cherry

class Foo {
  int foo = 42;
   foo int bar;
  this(int frop) {
    foo = frop;
  }
  int getFooAttr() {
    auto ffff = __traits(getAttributes, this.tupleof[1]);
    return ffff[0];
  }
}

void main() {
  Foo f = new Foo(43);
  f.getFooAttr();
}
Sep 27 2014
parent Marco Leise <Marco.Leise gmx.de> writes:
Am Sat, 27 Sep 2014 19:19:28 +0530
schrieb d coder via Digitalmars-d
<digitalmars-d puremagic.com>:

 DMD wont compile this. I get an error saying:
 /tmp/test.d(3): Error: need 'this' for 'foo' of type 'int'
 Failed: ["dmd", "-v", "-o-", "/tmp/test.d", "-I/tmp"]
 
 Is this a bug or is it illegal code?
 
 // Regards
 // Cherry
 
 class Foo {
   int foo = 42;
    foo int bar;
   this(int frop) {
     foo = frop;
   }
   int getFooAttr() {
     auto ffff = __traits(getAttributes, this.tupleof[1]);
     return ffff[0];
   }
 }
 
 void main() {
   Foo f = new Foo(43);
   f.getFooAttr();
 }
That's incorrect code. UDAs are attached to the item (the ctor in this case) at compile time and foo is neither compile time nor static, it is a class instance field. You would need to use "foo" and in getFooAttr() go through the getMember trait. -- Marco
Sep 27 2014