www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - template mixin bug?

reply "Simen Haugen" <simen norstat.no> writes:
I have a mixin like this:
template M() {
 int i() {
  return _i;
 }

 private:
 int _i;
}

But if I try to add a setter function to my class that uses the mixin, the 
compiler gets confused (seems like its hiding the mixed in function):
class C {
 mixin Mixin;
 void i(int a) {
  _i = a;
 }
}

void main() {
 C c = new C;
 c.i = 12;
 assert(c.i == 12);
}

Now it fails to compile if I try to use the get method from the mixin. If I 
don't use the method, it compiles.

t.d(29): function test.C.i (int) does not match parameter types ()
t.d(29): Error: expected 1 arguments, not 0
t.d(29): Error: void has no value
t.d(29): Error: incompatible types for (((c.i)()) == (12)): 'void' and 'int'

Is there a reason I cannot do this, or is it a compiler bug? I'm using dmd 
1.023. 
Nov 13 2007
next sibling parent Regan Heath <regan netmail.co.nz> writes:
Simen Haugen wrote:
 I have a mixin like this:
 template M() {
  int i() {
   return _i;
  }
 
  private:
  int _i;
 }
 
 But if I try to add a setter function to my class that uses the mixin, the 
 compiler gets confused (seems like its hiding the mixed in function):
 class C {
  mixin Mixin;
  void i(int a) {
   _i = a;
  }
 }
 
 void main() {
  C c = new C;
  c.i = 12;
  assert(c.i == 12);
 }
 
 Now it fails to compile if I try to use the get method from the mixin. If I 
 don't use the method, it compiles.
 
 t.d(29): function test.C.i (int) does not match parameter types ()
 t.d(29): Error: expected 1 arguments, not 0
 t.d(29): Error: void has no value
 t.d(29): Error: incompatible types for (((c.i)()) == (12)): 'void' and 'int'
 
 Is there a reason I cannot do this, or is it a compiler bug? I'm using dmd 
 1.023. 
 
 

Here is a workaround, change the mixin and add an alias: mixin M b; alias b.i i; As for whether it's a bug <shrug>. Regan
Nov 13 2007
prev sibling parent Matti Niemenmaa <see_signature for.real.address> writes:
Simen Haugen wrote:
 I have a mixin like this:
 template M() {
  int i() {
   return _i;
  }
 
  private:
  int _i;
 }
 
 But if I try to add a setter function to my class that uses the mixin, the 
 compiler gets confused (seems like its hiding the mixed in function):
 class C {
  mixin Mixin;
  void i(int a) {
   _i = a;
  }
 }
 
 void main() {
  C c = new C;
  c.i = 12;
  assert(c.i == 12);
 }
 
 Now it fails to compile if I try to use the get method from the mixin. If I 
 don't use the method, it compiles.
 
 t.d(29): function test.C.i (int) does not match parameter types ()
 t.d(29): Error: expected 1 arguments, not 0
 t.d(29): Error: void has no value
 t.d(29): Error: incompatible types for (((c.i)()) == (12)): 'void' and 'int'
 
 Is there a reason I cannot do this, or is it a compiler bug? I'm using dmd 
 1.023. 

http://digitalmars.com/d/1.0/template-mixin.html "If the name of a declaration in a mixin is the same as a declaration in the surrounding scope, the surrounding declaration overrides the mixin one". This means that your void i overrides the mixed-in i and, as you say, the mixed-in function is hidden. The page also mentions a workaround: "If a mixin has a MixinIdentifier, it can be used to disambiguate". You'll find the following works: class C { mixin M Foo; alias Foo.i i; void i(int a) { _i = a; } } -- E-mail address: matti.niemenmaa+news, domain is iki (DOT) fi
Nov 13 2007