Remix.run Logo
Mawr 2 days ago

Cute, essentially equivalent to Python's inner functions and Go's closures, e.g in Go:

    func foo(cfg_file string) (parsed, error) {
        config := func() {
            return json.Parse(cfg_file)
        }
        return config.parsed
    }
All of these are however poor solutions to the problem, because they're not true nested functions — they can access arbitrary variables defined outside their scope. Python at least restricts their modification, but Go doesn't. I'm guessing in Rust it's at least explicit in some way?

In any case, the real solution here is to simply allow proper nested functions that behave exactly like freestanding functions in that they can only access what's passed to them:

    func foo(cfg_file string) (parsed, error) {
        func config(cfg_file) config {
            return json.Parse(cfg_file)
        }
        return config.parsed
    }
This way you can actually reason about that block of code in isolation—same effect as when calling a freestanding function, except this doesn't expose the nested function to callers outside the parent function, which is valuable.