digitalmars.D.learn - auto functions not authorized inside main?
- Philippe Sigaud (16/16) Jun 27 2010 Is it defined somewhere that auto functions are not authorized inside ma...
- bearophile (4/6) Jun 27 2010 Maybe auto funcs are seen as instantiated templates, and templates can't...
- Rory McGuire (8/26) Jun 28 2010 Hope this isn't a stupid question, but how would you access this functio...
- BCS (5/18) Jun 28 2010 I would look almost the same to the user but should in fact be a normal ...
- Rory McGuire (7/24) Jun 28 2010 Ye I got it now.
- Rory McGuire (8/46) Jun 28 2010 Right! I get what you're saying, didn't realise because it was formatted...
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
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
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
Hello Rory,On Sun, 27 Jun 2010 17:17:25 +0200, Philippe Sigaud <philippe.sigaud gmail.com> wrote:I would look almost the same to the user but should in fact be a normal local function. -- ... <IXOYE><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;};
Jun 28 2010
On Mon, 28 Jun 2010 16:01:46 +0200, BCS <none anon.com> wrote:Hello Rory,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. -RoryOn Sun, 27 Jun 2010 17:17:25 +0200, Philippe Sigaud <philippe.sigaud gmail.com> wrote:I would look almost the same to the user but should in fact be a normal local function.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;};
Jun 28 2010
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 dovoid main() { string foo(string s) { return s;} }and notvoid 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.PhilippeRight! 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
Jun 28 2010









bearophile <bearophileHUGS lycos.com> 