Thread overview | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 27, 2011 Nested function declarations | ||||
---|---|---|---|---|
| ||||
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() What is the purpose of nested function declarations in D? Is it a good idea to just disallow them? Bye and thank you, bearophile |
January 28, 2011 Re: Nested function declarations | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | > What is the purpose of nested function declarations in D? Is it a good idea to just disallow them?
In theory they may be used to define mutually recursive nested functions, like in this example, but in practice this is not allowed:
import std.stdio: writeln;
void main() {
// Hofstadter Female and Male sequences
int M(int);
int F(int n) {
return n ? n - M(F(n - 1)) : 1;
}
int M(int n) { // test.d(11): Error: declaration M is already defined
return n ? n - F(M(n - 1)) : 0;
}
foreach (i; 0 .. 100)
writeln(F(i));
}
Bye,
bearophile
|
January 28, 2011 Re: Nested function declarations | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | Well, you might be linking to an external function /and/ don't want the function to be visible at module scope: void main() { extern(C) double func(); // linked from some C library.. double result = func(); } You don't have to worry too much about your first example, it might compile but it won't link: import std.math: sqrt; void main() { double sqrt(); double result = sqrt(); } Error 42: Symbol Undefined _D12externalTest4mainFZv4sqrtMFZd --- errorlevel 1 |
January 28, 2011 Re: Nested function declarations | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | "bearophile" <bearophileHUGS@lycos.com> a écrit dans le message de news: iht0ha$2avd$1@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. |
January 28, 2011 Re: Nested function declarations | ||||
---|---|---|---|---|
| ||||
Posted in reply to Akakima | On 28.01.2011 03:35, Akakima wrote:
> "bearophile"<bearophileHUGS@lycos.com> a �crit dans le message de news:
> iht0ha$2avd$1@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);
}
|
January 29, 2011 Re: Nested function declarations | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | bearophile napisał: > What is the purpose of nested function declarations in D? Is it a good idea to just disallow them? 1. Helper functions don't clutter the namespace. 2. Nested functions can access the outer function's stack frame. -- Tomek |
January 29, 2011 Re: Nested function declarations | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomek Sowiński | Tomek S.
> > What is the purpose of nested function declarations in D? Is it a good idea to just disallow them?
>
> 1. Helper functions don't clutter the namespace.
> 2. Nested functions can access the outer function's stack frame.
My question was not about "nested functions" but about "nested (empty) function declarations".
Bye,
bearophile
|
January 29, 2011 Re: Nested function declarations | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomek Sowiński | Tomek Sowiński napisał: > > What is the purpose of nested function declarations in D? Is it a good idea to just disallow them? > > 1. Helper functions don't clutter the namespace. > 2. Nested functions can access the outer function's stack frame. OK, I just noticed you asked about declarations, not nested functions in general. They're useful for testing: unittest { int foo(); static assert (is(ReturnType!foo == int)); } -- Tomek |
January 29, 2011 Re: Nested function declarations | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomek Sowiński | > They're useful for testing:
>
> unittest {
> int foo();
> static assert (is(ReturnType!foo == int));
> }
and else? is it worth?
|
January 29, 2011 Re: Nested function declarations | ||||
---|---|---|---|---|
| ||||
Posted in reply to dennis luehring | dennis luehring: > and else? is it worth? Andrej has shown me another possible purpose: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=24196 Surely they aren't the most useful feature of D language :-) But my guess is that removing them increases the size of the compiler a bit. I don't know if they are worth, I don't like them much, I guess they don't hurt too much. Bye, bearophile |
Copyright © 1999-2021 by the D Language Foundation