March 27, 2005 max(byte, byte) | ||||
---|---|---|---|---|
| ||||
Is this a bug? ---------------------------------------------------------------- $ cat max.d import std.math2; int main() { byte a, b; return max(a,b); } ---------------------------------------------------------------- $ dmd max.d max.d(6): function std.math2.max called with argument types: (byte,byte) matches both: std.math2.max(int,int) and: std.math2.max(real,real) ---------------------------------------------------------------- There's no partial ordering on type? e.g byte < short < int < real, so the nearest one should be picked up? BTW, the implementation in src/phobos/std/math2.d is ugly: int max(int a, int b) ... long max(long a, long b) ... real max(real a, real b) ... Why can't D's template be use here? |
March 28, 2005 Re: max(byte, byte) | ||||
---|---|---|---|---|
| ||||
Posted in reply to bug | In article <d25ana$163u$1@digitaldaemon.com>, bug@d.com says... > >Is this a bug? > >---------------------------------------------------------------- >$ cat max.d >import std.math2; > >int main() >{ >byte a, b; >return max(a,b); >} > >---------------------------------------------------------------- >$ dmd max.d >max.d(6): function std.math2.max called with argument types: >(byte,byte) >matches both: >std.math2.max(int,int) >and: >std.math2.max(real,real) > >---------------------------------------------------------------- > >There's no partial ordering on type? e.g byte < short < int < real, so the nearest one should be picked up? > >BTW, the implementation in src/phobos/std/math2.d >is ugly: > >int max(int a, int b) ... >long max(long a, long b) ... >real max(real a, real b) ... > >Why can't D's template be use here? > D's type-matching for function signatures is dead-simple. Either it finds an exact match, or it matches any (and all) methods with a valid implicit cast to the parameter type. In this case, byte can be implicitly cast to int *or* real, so it's complaining that your code is ambiguous. You wouldn't want to have some implicit cast ordering to resolve these matters, as that would lead to hard to debug code; in your example above, how would you know which max() function is being called?. IMO, we all want fewer such rules, rather than more. Anyway, the solution here is simple: use a shim function to remove the ambiguity. > byte max(byte a, byte b){ return cast(byte)max(cast(int)a,cast(int)b); } .. or use the casts directly in your code. - EricAnderton at yahoo |
Copyright © 1999-2021 by the D Language Foundation