digitalmars.D - template mixin bug?
- Simen Haugen (29/29) Nov 13 2007 I have a mixin like this:
- Regan Heath (6/43) Nov 13 2007 Here is a workaround, change the mixin and add an alias:
- Matti Niemenmaa (17/52) Nov 13 2007 http://digitalmars.com/d/1.0/template-mixin.html
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
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
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









Regan Heath <regan netmail.co.nz> 