www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - auto functions not authorized inside main?

reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
Is it defined somewhere that auto functions are not authorized inside main?

void main()
{
    auto fun(string s) { return s;} // this does not compile
}

error:

main.d|6|found 's' when expecting ')'|
main.d|6|semicolon expected, not ')'|
main.d|6|found ')' instead of statement|
main.d|7|unrecognized declaration|
||=== Build finished: 4 errors, 0 warnings ===|


So it's not even parsed?

I couldn't find a bugzilla entry for this and I cannot believe no one ever
tried to put an auto fun inside main!

Is that part of the spec?

Philippe
Jun 27 2010
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
Philippe Sigaud:
 I couldn't find a bugzilla entry for this and I cannot believe no one ever
 tried to put an auto fun inside main!

Maybe auto funcs are seen as instantiated templates, and templates can't be defined inside functions. Anyway, I think you can file this as enhancement request. Bye, bearophile
Jun 27 2010
prev sibling next sibling parent reply "Rory McGuire" <rmcguire neonova.co.za> writes:
On Sun, 27 Jun 2010 17:17:25 +0200, Philippe Sigaud  
<philippe.sigaud gmail.com> wrote:

 Is it defined somewhere that auto functions are not authorized inside  
 main?

 void main()
 {
     auto fun(string s) { return s;} // this does not compile
 }

 error:

 main.d|6|found 's' when expecting ')'|
 main.d|6|semicolon expected, not ')'|
 main.d|6|found ')' instead of statement|
 main.d|7|unrecognized declaration|
 ||=== Build finished: 4 errors, 0 warnings ===|


 So it's not even parsed?

 I couldn't find a bugzilla entry for this and I cannot believe no one  
 ever
 tried to put an auto fun inside main!

 Is that part of the spec?

 Philippe

Hope this isn't a stupid question, but how would you access this function if it did work? Would it be fun("asdf")? Is this just shorthand for: auto fun = function(string s) {return s;}; -Rory
Jun 28 2010
next sibling parent BCS <none anon.com> writes:
Hello Rory,

 On Sun, 27 Jun 2010 17:17:25 +0200, Philippe Sigaud
 <philippe.sigaud gmail.com> wrote:
 
 void main()
 {
 auto fun(string s) { return s;} // this does not compile
 }

Hope this isn't a stupid question, but how would you access this function if it did work? Would it be fun("asdf")? Is this just shorthand for: auto fun = function(string s) {return s;};

I would look almost the same to the user but should in fact be a normal local function. -- ... <IXOYE><
Jun 28 2010
prev sibling parent "Rory McGuire" <rmcguire neonova.co.za> writes:
On Mon, 28 Jun 2010 16:01:46 +0200, BCS <none anon.com> wrote:

 Hello Rory,

 On Sun, 27 Jun 2010 17:17:25 +0200, Philippe Sigaud
 <philippe.sigaud gmail.com> wrote:

 void main()
 {
 auto fun(string s) { return s;} // this does not compile
 }

Hope this isn't a stupid question, but how would you access this function if it did work? Would it be fun("asdf")? Is this just shorthand for: auto fun = function(string s) {return s;};

I would look almost the same to the user but should in fact be a normal local function.

Ye I got it now. My brain was interpreting it as a delegate that wasn't being assigned to anything for some reason. Now I get that it is just return type inferance doesn't work for inner functions. -Rory
Jun 28 2010
prev sibling parent "Rory McGuire" <rmcguire neonova.co.za> writes:
------------BwINXmvaJv6NmuMKFoiPoY
Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes
Content-Transfer-Encoding: 7bit

On Mon, 28 Jun 2010 16:07:43 +0200, Philippe Sigaud  
<philippe.sigaud gmail.com> wrote:

 On Mon, Jun 28, 2010 at 15:40, Rory McGuire <rmcguire neonova.co.za>  
 wrote:

 void main()
 {
    auto fun(string s) { return s;} // this does not compile
 }



 Hope this isn't a stupid question, but how would you access this  
 function if it did work?
 Would it be fun("asdf")?


 Yes, that's what I had in mind. Basically, just using it as any other  
 auto inner function.

 void main()
 {
 auto fun(string s) { return s;}
 auto s = fun("abc");
 auto t = fun("def");
 }

 Is this just shorthand for:
 auto fun = function(string s) {return s;};


 That'd be about the same, yes. Fact is, I don't really _need_ this, I  
 was just astonished to be bitten by this.
 Why can I do

 void main()
 {
    string foo(string s) { return s;}
 }

 and not

 void main()
 {
    auto foo(string s) { return s;}
 }

 ?

 ***

 OK, I tested it some more, and it seems you cannot define auto function  
 inside any other function. So auto function cannot be inner functions.  
 I'm quite astonished I never did that when using D, but OK.

 I filed a bug report, at least to update the docs. It's bug #4401.

 Philippe

Right! I get what you're saying, didn't realise because it was formatted more how I would format a anon delegate. You're saying "surely the compiler can infer the return type for a inner function just as much as it can infer the return type of a normal function. Must be a compiler bug. -Rory ------------BwINXmvaJv6NmuMKFoiPoY Content-Type: multipart/related; boundary=----------BwINXmvaJv6NmuGpRzpuMD ------------BwINXmvaJv6NmuGpRzpuMD Content-Type: text/html; charset=utf-8 Content-ID: <op.1277738995041.8c854d546d5dad86 10.0.0.88> Content-Transfer-Encoding: Quoted-Printable <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <HTML> <HEAD></HEAD> <BODY style=3D"font-family:'DejaVu Sans Mono'; font-size:12px"><DIV>On Mon,= 28 Jun 2010 16:07:43 +0200, Philippe Sigaud &lt;philippe.sigaud gmail.com&= gt; wrote:<BR><BR><BLOCKQUOTE style=3D"margin: 0 0 0.80ex; border-left: #00= 00FF 2px solid; padding-left: 1ex"><DIV class=3D"gmail_quote">On Mon, Jun 2= 8, 2010 at 15:40, Rory McGuire <SPAN dir=3D"ltr">&lt;<A href=3D"mailto:rmcg= uire neonova.co.za">rmcguire neonova.co.za</A>&gt;</SPAN> wrote:<BR><BLOCKQ= UOTE class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left:= 1px solid rgb(204, 204, 204); padding-left: 1ex;"> <DIV><DIV class=3D"h5"><BR><BLOCKQUOTE class=3D"gmail_quote" style=3D"margi= n: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-le= ft: 1ex;"> void main()<BR> {<BR> =C2=A0 =C2=A0auto fun(string s) { return s;} // this does not compile<BR> }<BR><BR></BLOCKQUOTE> <BR></DIV></DIV> Hope this isn't a stupid question, but how would you access this function i= f it did work?<BR> Would it be fun("asdf")?<BR></BLOCKQUOTE><DIV><BR>Yes, that's what I had in= mind. Basically, just using it as any other auto inner function. <BR><BR>v= oid main()<BR>{<BR>auto fun(string s) { return s;}<BR>auto s =3D fun("abc")= ;<BR> auto t =3D fun("def");<BR>}<BR>=C2=A0<BR></DIV><BLOCKQUOTE class=3D"gmail_q= uote" style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 2= 04, 204); padding-left: 1ex;"> Is this just shorthand for:<BR> auto fun =3D function(string s) {return s;};</BLOCKQUOTE><DIV><BR>That'd be= about the same, yes. Fact is, I don't really _need_ this, I was just aston= ished to be bitten by this.<BR>Why can I do<BR> <BR>void main()<BR>{<BR>=C2=A0 =C2=A0 string foo(string s) { return s;}<BR>= }<BR><BR>and not<BR><BR>void main()<BR>{<BR>=C2=A0 =C2=A0 auto foo(string s= ) { return s;}<BR>} <BR><BR>?<BR><BR>***<BR><BR>OK, I tested it some more, = and it seems you cannot define auto function inside any other function. So = auto function cannot be inner functions. I'm quite astonished I never did t= hat when using D, but OK.<BR> <BR>I filed a bug report, at least to update the docs. It's bug #4401.<BR><= BR><BR>Philippe<BR><BR><BR><BR></DIV></DIV> </BLOCKQUOTE><BR>Right! I get what you're saying, didn't realise because it= was formatted more how I would format a anon delegate.</DIV><DIV>You're sa= ying "surely the compiler can infer the return type for a inner function ju= st as much as it can infer the return type of a normal function..<BR></DIV>= <DIV><BR></DIV><DIV>Must be a compiler bug.</DIV><DIV><BR></DIV><DIV>-Rory<= /DIV></BODY></HTML> ------------BwINXmvaJv6NmuGpRzpuMD-- ------------BwINXmvaJv6NmuMKFoiPoY--
Jun 28 2010