Overloading an imported function

Maxim Fomin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Oct 23 10:36:33 PDT 2015


On Wednesday, 21 October 2015 at 12:05:27 UTC, Shriramana Sharma 
wrote:
> import std.math;
> real round(real val, int prec)
> {
>     real pow = 10 ^^ prec;
>     return round(val * pow) / pow;
> }
>
> Trying to compile this I get:
>
> foo.d(5): Error: function foo.round (real val, int prec) is not 
> callable using argument types (real)
>
> When I've imported std.math which contains round(real), why is 
> the compiler complaining about not being able to call the 
> overload function defined in *this* module?
>
> I don't see anything in http://dlang.org/module.html that says 
> I cannot define an overload of an imported function. Did I miss 
> something?

My guess is that <filename>.round shadows math.round. But you can 
get desired behavior
by moving declaration of math.round inside scope of 
<filename>.round. This compiles:

real round(real val, int prec)
{
     import std.math;
     real pow = 10 ^^ prec;
     return round(val * pow) / pow;
}




More information about the Digitalmars-d-learn mailing list