www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - mixins logics

reply nail <nail_member pathlink.com> writes:
Hello. Look at the following code:

template FloatingCmp(T)
{
bit equal(T a, T b, T tolerance = T.epsilon)
{
return (b - tolerance <= a) && (b + tolerance >= a);
}

bit less(T a, T b, T tolerance = T.epsilon)
{
return b - tolerance > a;
}

bit greater(T a, T b, T tolerance = T.epsilon)
{
return b + tolerance < a;
}
}

mixin FloatingCmp!(float);
mixin FloatingCmp!(double);
mixin FloatingCmp!(real);



int main ( char[][] args )
{
float a = 1.f;
float b = 1.01f;

if (equal(a, b))
{
puts("Yo!");
}

getch();

return 1;
}

It doesn't compile. The error is file(line): function equal conflicts with
FloatingCmp!(double).equal at the same file(the same line). But if I'll manualy
overload this functions (without any templates) all would compile. What is
logics of unnamed mixins behaviour? Why it is not equivalent with manual
overload?
Oct 21 2004
next sibling parent "Thomas Kuehne" <eisvogel users.sourceforge.net> writes:
nail_member pathlink.com schrieb:
 Hello. Look at the following code:

 template FloatingCmp(T)
 {
 bit equal(T a, T b, T tolerance = T.epsilon)
 {
 return (b - tolerance <= a) && (b + tolerance >= a);
 }

 bit less(T a, T b, T tolerance = T.epsilon)
 {
 return b - tolerance > a;
 }

 bit greater(T a, T b, T tolerance = T.epsilon)
 {
 return b + tolerance < a;
 }
 }

 mixin FloatingCmp!(float);
 mixin FloatingCmp!(double);
 mixin FloatingCmp!(real);



 int main ( char[][] args )
 {
 float a = 1.f;
 float b = 1.01f;

 if (equal(a, b))
 {
 puts("Yo!");
 }

 getch();

 return 1;
 }

 It doesn't compile. The error is file(line): function equal conflicts with
 FloatingCmp!(double).equal at the same file(the same line). But if I'll manualy
 overload this functions (without any templates) all would compile. What is
 logics of unnamed mixins behaviour? Why it is not equivalent with manual
 overload?

the code below is from memory - thus might contain typos - but did work for me:
 alias mixin FloatingCmp!(float).equal myEqual;
 alias mixin FloatingCmp!(double).equal myEqual;
 alias mixin FloatingCmp!(real).equal myEqual;

This clearly seems to be a bug to me. Thomas
Oct 22 2004
prev sibling parent reply David Medlock <amedlock nospam.org> writes:
nail wrote:
<snip>
 It doesn't compile. The error is file(line): function equal conflicts with
 FloatingCmp!(double).equal at the same file(the same line). But if I'll manualy
 overload this functions (without any templates) all would compile. What is
 logics of unnamed mixins behaviour? Why it is not equivalent with manual
 overload?
 
 

I have seen this too. The behaviour is for mixin logic to silently do nothing if there is a corresponding function in the scope its used in, but apparently with some overloaded functions perhaps the first mixin is instantiated then the others do not get expanded?
Oct 22 2004
parent Sean Kelly <sean f4.ca> writes:
In article <clb2ur$11u$1 digitaldaemon.com>, David Medlock says...
nail wrote:
<snip>
 It doesn't compile. The error is file(line): function equal conflicts with
 FloatingCmp!(double).equal at the same file(the same line). But if I'll manualy
 overload this functions (without any templates) all would compile. What is
 logics of unnamed mixins behaviour? Why it is not equivalent with manual
 overload?

I have seen this too. The behaviour is for mixin logic to silently do nothing if there is a corresponding function in the scope its used in, but apparently with some overloaded functions perhaps the first mixin is instantiated then the others do not get expanded?

The easiest way to remember it is that "mixin" and "import" work the same way. Pretend that each template instantiation creates a different module signature. The resulting problem is that you're importing multiple modules with the same symbols. You have to use alias to get overloading to work right in this case. Sean
Oct 22 2004