Thread overview | ||||||
---|---|---|---|---|---|---|
|
October 21, 2004 mixins logics | ||||
---|---|---|---|---|
| ||||
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? |
October 22, 2004 Re: mixins logics | ||||
---|---|---|---|---|
| ||||
Posted in reply to nail | 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 |
October 22, 2004 Re: mixins logics | ||||
---|---|---|---|---|
| ||||
Posted in reply to nail | 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?
|
October 22, 2004 Re: mixins logics | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Medlock | 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 |
Copyright © 1999-2021 by the D Language Foundation