Discussion:
Mixin template functions are ignored in struct
tcak via Digitalmars-d-learn
2014-10-14 20:58:18 UTC
Permalink
I have written a struct and a mixin template, and that mixin
template is mixed into that struct as follows.

private mixin template TestCommonMethods(){
public bool apply( int d, int e ){
return false;
}
}

public struct Test{
public mixin TestCommonMethods;

public bool apply( char c ){
return true;
}
}

void main(){
Test t;
t.apply( 5, 3 );
}

---

For the line "t.apply( 5, 3 );", error is given saying that
"function test.apply(char c) is not callable".

---

For better testing, I added another function to template as
"public bool blah(){}", and called it in main, and it works. So,
thus this mean overloading is not supported with mixin templates?
anonymous via Digitalmars-d-learn
2014-10-14 21:10:01 UTC
Permalink
Post by tcak via Digitalmars-d-learn
I have written a struct and a mixin template, and that mixin
template is mixed into that struct as follows.
private mixin template TestCommonMethods(){
public bool apply( int d, int e ){
return false;
}
}
public struct Test{
public mixin TestCommonMethods;
public bool apply( char c ){
return true;
}
}
void main(){
Test t;
t.apply( 5, 3 );
}
---
For the line "t.apply( 5, 3 );", error is given saying that
"function test.apply(char c) is not callable".
---
For better testing, I added another function to template as
"public bool blah(){}", and called it in main, and it works.
So, thus this mean overloading is not supported with mixin
templates?
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
So, yes, the mixed in `apply` doesn't overload with the other one.

You can use an alias declaration to bring them together:

public struct Test{
public mixin TestCommonMethods Common;
alias apply = Common.apply;

public bool apply( char c ){
return true;
}
}
tcak via Digitalmars-d-learn
2014-10-14 21:21:32 UTC
Permalink
Post by anonymous via Digitalmars-d-learn
Post by tcak via Digitalmars-d-learn
I have written a struct and a mixin template, and that mixin
template is mixed into that struct as follows.
private mixin template TestCommonMethods(){
public bool apply( int d, int e ){
return false;
}
}
public struct Test{
public mixin TestCommonMethods;
public bool apply( char c ){
return true;
}
}
void main(){
Test t;
t.apply( 5, 3 );
}
---
For the line "t.apply( 5, 3 );", error is given saying that
"function test.apply(char c) is not callable".
---
For better testing, I added another function to template as
"public bool blah(){}", and called it in main, and it works.
So, thus this mean overloading is not supported with mixin
templates?
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
So, yes, the mixed in `apply` doesn't overload with the other
one.
public struct Test{
public mixin TestCommonMethods Common;
alias apply = Common.apply;
public bool apply( char c ){
return true;
}
}
Hmm, so it is not able to mix enough then. That's a weird
decision though. Anyway, that suggested usage is making my work
harder. I am putting that mixin in many struct and defining each
method one by one in that way doesn't seem like suitable to me.
Adam D. Ruppe via Digitalmars-d-learn
2014-10-14 21:41:32 UTC
Permalink
Anyway, that suggested usage is making my work harder. I am
putting that mixin in many struct and defining each method one
by one in that way doesn't seem like suitable to me.
You could rename the method in the struct then mixin the rest.
Like

private mixin template TestCommonMethods(){
public bool apply( int d, int e ){
return false;
}
}

public struct Test{
public mixin TestCommonMethods;

public bool apply2( char c ){ // now named apply2
return true;
}
}

void main(){
Test t;
t.apply( 5, 3 ); // works
t.apply2('c'); // also works
}



The mixin template might also define an apply function that just
forwards the call to the other name, similarly to how a final
method in a class or interface might call a virtual function to
allow customization.
Adam D. Ruppe via Digitalmars-d-learn
2014-10-14 21:37:53 UTC
Permalink
Post by tcak via Digitalmars-d-learn
So, thus this mean overloading is not supported with mixin
templates?
Nope, what happens is mixin stuff adds items by name. If you have
a variable or function in the object with the same name, it
overrides the mixin one entirely.

This is really useful for customizing the behavior of a mixin by
taking most but not all of its functions.
Daniel N via Digitalmars-d-learn
2014-10-15 02:37:25 UTC
Permalink
Post by tcak via Digitalmars-d-learn
I have written a struct and a mixin template, and that mixin
template is mixed into that struct as follows.
Use a normal mixin + token strings(q{}).

enum TestCommonMethods = q{
public bool apply( int d, int e ){
return false;
}
};

public struct Test{
mixin(TestCommonMethods);

public bool apply(char c){
return true;
}
}

void main(){
Test t;
t.apply( 5, 3 );
}

Loading...