www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - auto return for some recursive functions in C++11

reply "bearophile" <bearophileHUGS lycos.com> writes:
According to Wikipedia:
http://en.wikipedia.org/wiki/C%2B%2B14#Function_return_type_deduction

C++14 is able to compile code like this:


auto correct(int i) {
     if (i == 1)
         return i;
     else
         return correct(i - 1) + i;
}


But not like this:

auto correct(int i) {
     if (i != 1)
         return correct(i - 1) + i;
     else
         return i;
}


D isn't able to compile both. Is it a good idea to allow the 
first function in D too?

Bye,
bearophile
Jan 14 2015
next sibling parent reply ketmar via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Wed, 14 Jan 2015 22:29:08 +0000
bearophile via Digitalmars-d <digitalmars-d puremagic.com> wrote:

 According to Wikipedia:
 http://en.wikipedia.org/wiki/C%2B%2B14#Function_return_type_deduction
=20
 C++14 is able to compile code like this:
=20
=20
 auto correct(int i) {
      if (i =3D=3D 1)
          return i;
      else
          return correct(i - 1) + i;
 }
=20
=20
 But not like this:
=20
 auto correct(int i) {
      if (i !=3D 1)
          return correct(i - 1) + i;
      else
          return i;
 }
=20
=20
 D isn't able to compile both. Is it a good idea to allow the=20
 first function in D too?
=20
 Bye,
 bearophile
i'm pretty sure that D *SHOULD* compile the first sample. the spec says that return type for `auto` function is taken from the first `return` operator parser met. so for the second `return` the return type is already known. i think that this is not just "let it compile", but a bug in compiler. something should be fixed: either compiler or specs.
Jan 14 2015
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
ketmar:

 the spec says that return type for `auto` function is taken
 from the first `return` operator parser met. so for the
 second `return` the return type is already known.
Do you mean the C++14 spec or the D spec? The D specs I see are (from: http://dlang.org/function.html ):
 Auto Functions
 
 Auto functions have their return type inferred from any
 ReturnStatements in the function body.
 
 An auto function is declared without a return type. If it does 
 not
 already have a storage class, use the auto storage class.
 
 If there are multiple ReturnStatements, the types of them must 
 be
 implicitly convertible to a common type. If there are no
 ReturnStatements, the return type is inferred to be void.
Bye, bearophile
Jan 14 2015
parent ketmar via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Wed, 14 Jan 2015 23:07:57 +0000
bearophile via Digitalmars-d <digitalmars-d puremagic.com> wrote:

 ketmar:
=20
 the spec says that return type for `auto` function is taken
 from the first `return` operator parser met. so for the
 second `return` the return type is already known.
=20 Do you mean the C++14 spec or the D spec?
D spec. i'm not really interested in C++ progress, and certainly not that much that i'll read any of it's specs.
 The D specs I see are (from: http://dlang.org/function.html ):
=20
=20
 Auto Functions
=20
 Auto functions have their return type inferred from any
 ReturnStatements in the function body.
=20
 An auto function is declared without a return type. If it does=20
 not
 already have a storage class, use the auto storage class.
=20
 If there are multiple ReturnStatements, the types of them must=20
 be
 implicitly convertible to a common type. If there are no
 ReturnStatements, the return type is inferred to be void.
i bet that i read somewhere in specs that return type for `auto` function is defined by first `return` parser sees. yet strangely i can't find it right now. maybe that was changed recently, but i'm almost sure that this is not false memory of mine.
Jan 14 2015
prev sibling parent reply "Xinok" <xinok live.com> writes:
On Wednesday, 14 January 2015 at 22:37:22 UTC, ketmar via 
Digitalmars-d wrote:
 On Wed, 14 Jan 2015 22:29:08 +0000
 bearophile via Digitalmars-d <digitalmars-d puremagic.com> 
 wrote:

 According to Wikipedia:
 http://en.wikipedia.org/wiki/C%2B%2B14#Function_return_type_deduction
 
 C++14 is able to compile code like this:
 
 
 auto correct(int i) {
      if (i == 1)
          return i;
      else
          return correct(i - 1) + i;
 }
 
 
 But not like this:
 
 auto correct(int i) {
      if (i != 1)
          return correct(i - 1) + i;
      else
          return i;
 }
 
 
 D isn't able to compile both. Is it a good idea to allow the 
 first function in D too?
 
 Bye,
 bearophile
i'm pretty sure that D *SHOULD* compile the first sample. the spec says that return type for `auto` function is taken from the first `return` operator parser met. so for the second `return` the return type is already known. i think that this is not just "let it compile", but a bug in compiler. something should be fixed: either compiler or specs.
Well, the error the compiler prints is: Error: forward reference to inferred return type of function call 'correct' I played with it a bit and it seems to deduce a common type from all the return statements, which would be more in the style of D anyways (take type deduction for arrays). For example, the following code prints "float" rather than "int": import std.stdio; auto one(int i) { if(i > 0) return cast(int)i; else return cast(float)i; } void main() { writeln(typeid(typeof(one(10)))); }
Jan 14 2015
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Xinok:

 I played with it a bit and it seems to deduce a common type 
 from all the return statements,
Right, this works according to the D specs (but this is different from the feature I have suggested in the original post). Bye, bearophile
Jan 14 2015
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Xinok:

     auto one(int i)
     {
         if(i > 0)
             return cast(int)i;
         else
             return cast(float)i;
     }
With recent D2 compilers I suggest you to get used to write code like that like this: auto one(int i) { if(i > 0) return int(i); else return float(i); } It's better to minimize the usage of cast(). Bye, bearophile
Jan 14 2015
prev sibling next sibling parent ketmar via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Thu, 15 Jan 2015 00:05:30 +0000
Xinok via Digitalmars-d <digitalmars-d puremagic.com> wrote:

 Well, the error the compiler prints is:
=20
 Error: forward reference to inferred return type of function call=20
 'correct'
=20
 I played with it a bit and it seems to deduce a common type from=20
 all the return statements, which would be more in the style of D=20
 anyways (take type deduction for arrays). For example, the=20
 following code prints "float" rather than "int":
=20
      import std.stdio;
=20
      auto one(int i)
      {
          if(i > 0)
              return cast(int)i;
          else
              return cast(float)i;
      }
=20
      void main()
      {
          writeln(typeid(typeof(one(10))));
      }
i remember that there was some discussion about this, but it's all i can remember about it. ;-) there were some inconsistencies with deducing return type, AFAIR, but i don't remember if it leads to some fixes or not. if ony i wasn't so lazy and take some notes...
Jan 14 2015
prev sibling parent "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Thursday, 15 January 2015 at 00:05:32 UTC, Xinok wrote:
 I played with it a bit and it seems to deduce a common type 
 from all the return statements, which would be more in the 
 style of D anyways (take type deduction for arrays).
Yes, this has changed some time ago, the specs are already updated: http://dlang.org/function.html#auto-functions Given that the compiler already uses the common type, maybe it should just disregard recursive uses during inference if there is more than one return.
Jan 15 2015
prev sibling parent reply "MattCoder" <stop spam.com> writes:
On Wednesday, 14 January 2015 at 22:29:09 UTC, bearophile wrote:
 According to Wikipedia:
 http://en.wikipedia.org/wiki/C%2B%2B14#Function_return_type_deduction

 C++14 is able to compile code like this:


 auto correct(int i) {
     if (i == 1)
         return i;
     else
         return correct(i - 1) + i;
 }
 ...
According to the wiki link: "If multiple return expressions are used in the function's implementation, then they must all deduce the same type." So this will NOT work: auto Correct(int i) { if (i == 1){ return (int)5; }else{ return (float)10; } } Error: prog.cpp: In function 'auto Correct(int)': prog.cpp:8:19: error: inconsistent deduction for 'auto': 'int' and then 'float' return (float)10; ^ So could you please tell me a good use case to for using "return type deduction"? Matheus.
Jan 14 2015
parent reply "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
On Thursday, 15 January 2015 at 00:13:38 UTC, MattCoder wrote:
 So could you please tell me a good use case to for using "return
 type deduction"?
Templates.
Jan 14 2015
next sibling parent ketmar via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Thu, 15 Jan 2015 00:24:07 +0000
via Digitalmars-d <digitalmars-d puremagic.com> wrote:

 On Thursday, 15 January 2015 at 00:13:38 UTC, MattCoder wrote:
 So could you please tell me a good use case to for using "return
 type deduction"?
=20 Templates.
and returning voldemort types.
Jan 14 2015
prev sibling parent "MattCoder" <stop spam.com> writes:
On Thursday, 15 January 2015 at 00:24:09 UTC, Ola Fosheim Grøstad 
wrote:
 On Thursday, 15 January 2015 at 00:13:38 UTC, MattCoder wrote:
 So could you please tell me a good use case to for using 
 "return
 type deduction"?
Templates.
Thanks! - In fact after posting I took a look and found this: http://stackoverflow.com/questions/15737223/when-should-i-use-c14-automatic-return-type-deduction Matheus.
Jan 14 2015