why compiler try use my sqrt not std.math?
Cym13
cpicard at purrfect.fr
Wed May 8 08:48:40 UTC 2019
On Wednesday, 8 May 2019 at 08:28:57 UTC, KnightMare wrote:
> so compiler thinks and try convert my code upper to
> ulong sqrt1( ulong n ) {
> return cast( ulong )ceil<float OR real?>( @ret(ulong)my_sqrt(
> cast( ulong )real( n )));
> }
> why that? I didnt ask that.
>
> yes, I know that I can add inside sqrt1
> import std.math : sqrt;
> but.. WHY?
First of all, such questions are better suited for the Learn
section if you want quality answers.
When you define a function it's reasonnable to assume that you
want to use it, that's probably why D (and any language I know
really) resolves function names from the most local scope to the
largest one.
If it worked the other way arround you could define a function
like byLine thinking that this name is safe and be equally
confused when the program tries making web requests because you
have no clue that it's actually already defined in std.net.curl.
Here at least you're confused but you have the function before
you so that confusion shouldn't last as long.
It's also true inside that function's definition: recursive
functions are common and they need that mechanics to work.
Now, in my opinion, the right way to fix your code (short of
choosing another name) is to specify the function you wish to use
explicitely:
import std.math;
ulong sqrt(ulong n) {
return cast(ulong) std.math.sqrt(real(n));
}
And for reference, should you want to name the current module's
implementation of sqrt, you would do as such:
ulong sqrt(ulong n) {
return cast(ulong) .sqrt(real(n));
}
But of course here it would cause infinite recursion and stack
overflow.
More information about the Digitalmars-d
mailing list