Nested function declarations

wrzosk dprogr at gmail.com
Fri Jan 28 01:52:12 PST 2011


On 28.01.2011 03:35, Akakima wrote:
> "bearophile"<bearophileHUGS at lycos.com>  a �crit dans le message de news:
> iht0ha$2avd$1 at digitalmars.com...
>> This D2 code:
>>
>> import std.math: sqrt;
>> void main() {
>>     double sqrt();
>>     double result = sqrt(9.0);
>> }
>>
>> Generates the errors:
>> test.d(4): Error: function test.main.sqrt () is not callable using
>> argument types (double)
>> test.d(4): Error: expected 0 arguments, not 1 for non-variadic function
>> type double()
>>
>
> --- This one compiles, but does not link:
>
> import std.math: sqrt;
> void main()
> {
>    double sqrt(double x);
>    double result = sqrt(9.0);
> }
>
>
> --- And this one compiles and links ok.
>
> import std.math: sqrt;
> void main()
> {
>    double sqrt(double x)  {    return 1.0;  }
>    double result = sqrt(9.0);
> }
>
> So, with the appropriate prototype it compiles, but
> there is a conflict with the sqrt() already defined in Phobos.
>
>
>

This works:

import std.math: sqrt;
import std.stdio;
void main()
{
   double sqrt(double x)  {    return 1.0;  }
   double result = .sqrt(9.0);
   writeln(result);
}


More information about the Digitalmars-d-learn mailing list