Function hijack on selective import
rumbu
rumbu at rumbu.ro
Tue Dec 26 15:48:14 UTC 2017
Is there anyway to extend an existing function to accept custom
data types?
****************
Option 1 - global import of std.math
import std.math;
struct Custom {}
int signbit(T)(T x) if (is(T == Custom))
{
return 0;
}
Custom c;
assert(signbit(c) == 0);
assert(signbit(-1.0) == 1);
Error: template main.signbit cannot deduce function from argument
types !()(double), candidates are:
main.signbit(T)(T x) if (is(T == Custom))
*******************
Option 2 - selective import of std.math
import std.math : signbit;
struct Custom {}
int signbit(T)(T x) if (is(T == Custom))
{
return 0;
}
Custom c;
assert(signbit(c) == 0);
assert(signbit(-1.0) == 1);
main.signbit called with argument types (Custom) matches both:
\..\src\phobos\std\math.d(5721):
std.math.signbit!(Custom).signbit(Custom x)
and:
main.signbit!(Custom).signbit(Custom x)
More information about the Digitalmars-d-learn
mailing list