What are the problems that are mitigated by not allowing nested function declarations in Go?

FunctionGoDeclaration

Function Problem Overview


Lambdas work as expected:

func main() {
    inc := func(x int) int { return x+1; }
}

However, the following declaration inside a declaration is not allowed:

func main() {
    func inc(x int) int { return x+1; }
}

For what reason are nested functions not allowed?

Function Solutions


Solution 1 - Function

I think there are 3 reasons why this obvious feature isn't allowed

  1. It would complicate the compiler slightly. At the moment the compiler knows all functions are at the top level.
  2. It would make a new class of programmer error - you could refactor something and accidentally nest some functions.
  3. Having a different syntax for functions and closures is a good thing. Making a closure is potentially more expensive than making a function so you should know you are doing it.

Those are just my opinions though - I haven't seen an official pronouncement from the language designers.

Solution 2 - Function

Sure they are. You just have to assign them to a variable:

func main() {
    inc := func(x int) int { return x+1; }
}

Solution 3 - Function

> Frequently Asked Questions (FAQ) > > Why does Go not have feature X? > > Every language contains novel features and omits someone's favorite > feature. Go was designed with an eye on felicity of programming, speed > of compilation, orthogonality of concepts, and the need to support > features such as concurrency and garbage collection. Your favorite > feature may be missing because it doesn't fit, because it affects > compilation speed or clarity of design, or because it would make the > fundamental system model too difficult. > > If it bothers you that Go is missing feature X, please forgive us and > investigate the features that Go does have. You might find that they > compensate in interesting ways for the lack of X.

What would justify the complexity and expense of adding nested functions? What do yau want to do that you can't do without nested functions? Et cetera.

Solution 4 - Function

Here's a way to implement nested functions and functions within nested functions

package main

import "fmt"

func main() {

    nested := func() {
	    fmt.Println("I am nested")

	    deeplyNested := func() {
		        fmt.Println("I am deeply nested")
	        }

	    deeplyNested()
    }

    nested()
}

Solution 5 - Function

Nested functions are allowed in Go. You just need to assign them to local variables within the outer function, and call them using those variables.

Example:

func outerFunction(iterations int, s1, s2 string) int {
    someState := 0
    innerFunction := func(param string) int {
        // Could have another nested function here!
        totalLength := 0
        // Note that the iterations parameter is available
        // in the inner function (closure)
        for i := 0; i < iterations; i++) {
            totalLength += len(param)
        }
        return totalLength
    }
    // Now we can call innerFunction() freely
    someState = innerFunction(s1)
    someState += innerFunction(s2)
    return someState
}
myVar := outerFunction(100, "blah", "meh")

Inner functions are often handy for local goroutines:

func outerFunction(...) {
    innerFunction := func(...) {
        ...
    }
    go innerFunction(...)
}

Solution 6 - Function

You just have to call it immediately by adding () to the end.

func main() {
    func inc(x int) int { return x+1; }()
}

Edit: cannot have function name...so it's just a lambda func getting called right away:

func main() {
    func(x int) int { return x+1; }()
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncorazzaView Question on Stackoverflow
Solution 1 - FunctionNick Craig-WoodView Answer on Stackoverflow
Solution 2 - FunctionMatt WilliamsonView Answer on Stackoverflow
Solution 3 - FunctionpeterSOView Answer on Stackoverflow
Solution 4 - FunctionKoala3View Answer on Stackoverflow
Solution 5 - FunctionvthorsteinssonView Answer on Stackoverflow
Solution 6 - FunctionNickView Answer on Stackoverflow