Re: Nested function declarations

2011-01-30 Thread Dan Olson
dennis luehring dl.so...@gmx.net writes:

 They're useful for testing:

 unittest {
  int foo();
  static assert (is(ReturnType!foo == int));
 }

 and else? is it worth?

Don't class function declarations have the same issue?  You can declare
but all you'll get is a link error.  Unless there some way like C++ to
provide a definition elsewhere.

class C
{
   int foo();
}

void main()
{
}

superarray_error.obj(superarray_error) 
 Error 42: Symbol Undefined _D16superarray_error1C3fooMFZi
--- errorlevel 1


Re: Nested function declarations

2011-01-29 Thread bearophile
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


Re: Nested function declarations

2011-01-29 Thread 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



Re: Nested function declarations

2011-01-29 Thread dennis luehring

They're useful for testing:

unittest {
int foo();
static assert (is(ReturnType!foo == int));
}


and else? is it worth?


Re: Nested function declarations

2011-01-29 Thread bearophile
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.learnarticle_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


Re: Nested function declarations

2011-01-29 Thread Andrej Mitrovic
Well, I've never used this actually. If I have some external C
functions I either put them in a separate module and then import them
in when necessary, or wrap them in a struct/class. Whether or not
they're of any use in scoped declarations is beyond me. :)


Re: Nested function declarations

2011-01-28 Thread wrzosk

On 28.01.2011 03:35, Akakima wrote:

bearophilebearophileh...@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);
}


Re: Nested function declarations

2011-01-27 Thread 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


Re: Nested function declarations

2011-01-27 Thread Andrej Mitrovic
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


Re: Nested function declarations

2011-01-27 Thread Akakima

bearophile bearophileh...@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.