Comment by gwd
1 day ago
So sometimes you want it lexical scope, and sometimes function scope; For example, maybe you open a bunch of files in a loop and need them all open for the rest of the function.
Right now it's function scope; if you need it lexical scope, you can wrap it in a function.
Suppose it were lexical scope and you needed it function scope. Then what do you do?
Making it lexical scope would make both of these solvable, and would be clear for anyone reading it.
You can just introduce a new scope wherever you want with {} in sane languages, to control the required behavior as you wish.
You can start a new scope with `{}` in go. If I have a bunch of temp vars I'll declare the final result outside the braces and then do the work inside. But lately days I'll just write a function. It's clearer and easier to test.
Currently, you can write
When it's lexically scoped, you'd need to add some variable. Not that that happens a lot, but a lexically scoped defer isnt needed often either.
Having lexicial scope it is same as -> defer fn{if(some condition) x() }() within scope.
1 reply →
What's an example of where you'd need to do that?
I can't recall ever needing that (but that might just be because I'm used to lexical scoping for defer-type constructs / RAII).
1 reply →
I frequently use that pattern. For example, something like this:
> Suppose it were lexical scope and you needed it function scope. Then what do you do?
Defer a bulk thing at the function scope level, and append files to an array after opening them.
That seems like more work, and less readability, than sticking in the extra function.
Would be nice to have both options though. Why not a “defer” package?
I never wanted function-scope defer, not sure what would be the usecase, but if there was one, you could just do what the other comments suggested.
Really? I find the opposite is true. If I need lexical scope then I’d just write, for example
The reason I might want function scope defer is because there might be a lot of different exit points from that function.
With lexical scope, there’s only three ways to safely jump the scope:
1. reaching the end of the procedure, in which case you don’t need a defer)
2. A ‘return’, in which case you’re also exiting the function scope
3. a ‘break’ or ‘continue’, which admittedly could see the benefit of a lexical scope defer but they’re also generally trivial to break into their own functions; and arguably should be if your code is getting complex enough that you’ve got enough branches to want a defer.
If Go had other control flows like try/catch, and so on and so forth, then there would be a stronger case for lexical defer. But it’s not really a problem for anyone aside those who are also looking for other features that Go also doesn’t support.
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.
1 reply →
Skipping defer ignores panics, with problems explained in the tfa.
1 reply →
You do what the compiler has to do under the hood: at the top of the function create a list of open files, and have a defer statement that loops over the list closing all of the files. It's really not a complicated construct.
defer { close all the files in the collection }
?
OK, what happens now if you have an error opening one of those files, return an error from inside the for loop, and forget to close the files you'd already opened?
You put the files in the collection as you open them, and you register the defer before opening any of them. It works fine. Defer should be lexically scoped.