← Back to context

Comment by desolate_muffin

1 day ago

I don't write Go, but reading this, it feels like you don't actually need functional scoping at all. In Java, I would simply write:

try (SomeResource foo = SomeResource.open()) { method(foo); }

or

public void method() { try(...) { // all business logic wrapped in try-with-resources } }

To me it seems like lexical scoping can accomplish nearly everything functional scoping can, just without any surprising behavior.

Minor nit but the second example is ostensibly still just functional scoping (as in not literally but more pragmatically) because you don’t contain any branching outside of the try block within your method.

But that’s a moot point because I appreciate it’s just an example. And, more importantly, Go doesn’t support the kind of control flows you’re describing anyway (as I said in my previous post).

A lot of the comments here about ‘defer’ make sense in other languages that have different idioms and features to Go. But they don’t apply directly to Go because you’d have to make other changes to the language first (eg implementing try blocks).