Syntax of nested functions

4,175 views
Skip to first unread message

Evan Farrer

unread,
Nov 28, 2009, 1:04:29 AM11/28/09
to golan...@googlegroups.com
Is there a reason why Go doesn't support the following syntax for nested functions?

func main() {
func world () {
Printf("world\n")
};

Printf("hello ");
world()
}


I know that the above code can be modified so it will compile by declaring an
anonymous function and assigning it to a local variable as follows:
func main() {
world := func () {
Printf("world\n")
};

Printf("hello ");
world()
}

The modified version is fine, but it does feel weird to have to use different
syntax when defining a function in the file block vs. in function block. Of
course I may just have some dumb syntax error in the first example.

Evan Farrer

Dimiter "malkia" Stanev

unread,
Nov 28, 2009, 2:17:15 AM11/28/09
to golang-nuts
Actually I'm thinking the other way around, that

func main () {
}

really is:

main := func () {
}

and that sugar (func main ()) works only on top-level expressions such
as main().

TerryP

unread,
Nov 28, 2009, 10:19:07 AM11/28/09
to golang-nuts

Honestly, I'm just thankful that `nested functions` are easier then in
C :-D.

I'm much to accustomed to how Perl does closures and allows assigning
other-wise anonymous subroutines to a variable via the 'sub' thing, to
complain about having to do varname := func ....; which still beats C
hands down. Proper closures + anonymous functions + references := fun
fun fun.

Perhaps the syntax Go uses, saves it from having to mangle names of
nested named function, let along having to explain what this does if
they don't employ some form of name mangling idea:

func x() {
}

func main() {
func x() {
}
}

Either that, or maybe the engineers liked Perl or Lisp.... hehe

--
TerryP
Just Another Computer Geek

yuk...@gmail.com

unread,
Nov 29, 2009, 8:48:01 AM11/29/09
to TerryP, golang-nuts
Another problem of having to use the funcname := func() ... syntax is that you must forward-declare functions that call each other.

For example you must write

a := func(x int) {
    ...
    b(x - 1);
};

b := func(y int) {
    a(y - 2);
    ...
};

by writing:

var a func(int);
var b func(int);

above those line, which seems a fragile thing, because you need to edit two places if you need to change the parameter/returnvalue list.

SnakE

unread,
Nov 30, 2009, 1:23:56 PM11/30/09
to yuk...@gmail.com, TerryP, golang-nuts

Another problem of having to use the funcname := func() ... syntax is that you must forward-declare functions that call each other.

I don't know about languages which would allow to use a nested function before declaration.  It could be just my ignorance though.

Ben Tilly

unread,
Nov 30, 2009, 2:16:44 PM11/30/09
to SnakE, yuk...@gmail.com, TerryP, golang-nuts
Virtually every dynamic language allows this. Of course there it is
easy because by the time the call is looked up at run time, both
definitions exist.

But the Hugs compiler for Haskell provides an example of a strongly
typed static language that allows this.
http://cvs.haskell.org/Hugs/pages/users_guide/haskell98.html has an
example of two mutually recursive functions which call each other with
the type declaration being automatically correctly figured out by the
compiler.

However the design of that language is sufficiently different from Go
that I would be surprised to see that kind of capacity added to Go.

Cheers,
Ben

yuk...@gmail.com

unread,
Dec 1, 2009, 10:23:55 AM12/1/09
to SnakE, TerryP, golang-nuts
2009/12/1 SnakE <snake...@gmail.com>

2009/11/29 yuk...@gmail.com <yuk...@gmail.com>

Another problem of having to use the funcname := func() ... syntax is that you must forward-declare functions that call each other.

I don't know about languages which would allow to use a nested function before declaration.  It could be just my ignorance though.

There is. It's called Go. 
But only on top level functions, like

package xyz
func a() { b() }
func b() { a() }

Now I (and hopefully we) wish this syntax is also allowed inside a func.

Reply all
Reply to author
Forward
0 new messages