www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Bug in Auto Functions and Template? Or Am I Just Crazy...

reply "Michael" <pongad gmail.com> writes:
Hello all,
   I came across some weird behaviors yesterday and I can't figure 
out what it's about.

(1)
auto factorial(int n) {
	if (n < 2) return 1;
	return n * factorial(n-1);
}

The compiler complained about "forward declaration of factorial". 
If I change the return type to int, the problem goes away. I can 
understand that with the recursion, it might be impossible for 
the compiler to deduce the type of factorial and so it compile 
errors, but I don't see anything about that on the Language 
Reference. Is this the intended behavior?

(2)
auto stuff(T)(T[] arr) {
	auto inner(T[] s) {
		s[0] = 0;
	}
	arr.inner();
}
This time the compiler complained about 'inner' not being 
defined. This time I have no idea what is wrong.

Thanks for your help!
Michael
Jun 27 2012
next sibling parent "Tobias Pankrath" <tobias pankrath.net> writes:
On Wednesday, 27 June 2012 at 09:07:57 UTC, Michael wrote:
 Hello all,
   I came across some weird behaviors yesterday and I can't 
 figure out what it's about.

 (1)
 auto factorial(int n) {
 	if (n < 2) return 1;
 	return n * factorial(n-1);
 }

 The compiler complained about "forward declaration of 
 factorial". If I change the return type to int, the problem 
 goes away. I can understand that with the recursion, it might 
 be impossible for the compiler to deduce the type of factorial 
 and so it compile errors, but I don't see anything about that 
 on the Language Reference. Is this the intended behavior?

 (2)
 auto stuff(T)(T[] arr) {
 	auto inner(T[] s) {
 		s[0] = 0;
 	}
 	arr.inner();
 }
 This time the compiler complained about 'inner' not being 
 defined. This time I have no idea what is wrong.

 Thanks for your help!
 Michael

(1) is intended bahaviour, though I do think that the compiler could be smarter. At least the cases, that could be tail-call-optimize could also be inferred. (2) looks like a bug to me
Jun 27 2012
prev sibling next sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 06/27/2012 11:07 AM, Michael wrote:
 Hello all,
    I came across some weird behaviors yesterday and I can't figure out
 what it's about.

 (1)
 auto factorial(int n) {
      if (n < 2) return 1;
      return n * factorial(n-1);
 }

 The compiler complained about "forward declaration of factorial". If I
 change the return type to int, the problem goes away. I can understand
 that with the recursion, it might be impossible for the compiler to
 deduce the type of factorial and so it compile errors, but I don't see
 anything about that on the Language Reference. Is this the intended
 behavior?

Apparently the compiler attempts to type combine the types of the return statements. This does not match the language documentation. I think it should just use the type of the first return statement.
 (2)
 auto stuff(T)(T[] arr) {
      auto inner(T[] s) {
          s[0] = 0;
      }
      arr.inner();
 }
 This time the compiler complained about 'inner' not being defined. This
 time I have no idea what is wrong.

 Thanks for your help!
 Michael

This is expected behaviour. UFCS only works with module scope functions.
Jun 27 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 06/27/2012 01:24 PM, Timon Gehr wrote:
 On 06/27/2012 11:07 AM, Michael wrote:
 Hello all,
    I came across some weird behaviors yesterday and I can't figure out
 what it's about.

 (1)
 auto factorial(int n) {
      if (n < 2) return 1;
      return n * factorial(n-1);
 }

 The compiler complained about "forward declaration of factorial". If I
 change the return type to int, the problem goes away. I can understand
 that with the recursion, it might be impossible for the compiler to
 deduce the type of factorial and so it compile errors, but I don't see
 anything about that on the Language Reference. Is this the intended
 behavior?

Apparently the compiler attempts to type combine the types of the return statements. This does not match the language documentation. I think it should just use the type of the first return statement. ...

http://d.puremagic.com/issues/show_bug.cgi?id=8307
Jun 27 2012
prev sibling parent "Michael" <pongad gmail.com> writes:
Good I know I spotted something _real_ this time!

On Wednesday, 27 June 2012 at 11:55:11 UTC, Timon Gehr wrote:
 On 06/27/2012 01:24 PM, Timon Gehr wrote:
 On 06/27/2012 11:07 AM, Michael wrote:
 Hello all,
   I came across some weird behaviors yesterday and I can't 
 figure out
 what it's about.

 (1)
 auto factorial(int n) {
     if (n < 2) return 1;
     return n * factorial(n-1);
 }

 The compiler complained about "forward declaration of 
 factorial". If I
 change the return type to int, the problem goes away. I can 
 understand
 that with the recursion, it might be impossible for the 
 compiler to
 deduce the type of factorial and so it compile errors, but I 
 don't see
 anything about that on the Language Reference. Is this the 
 intended
 behavior?

Apparently the compiler attempts to type combine the types of the return statements. This does not match the language documentation. I think it should just use the type of the first return statement. ...

http://d.puremagic.com/issues/show_bug.cgi?id=8307

Jun 28 2012