▲ | gwd 4 days 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? | |||||||||||||||||||||||||||||||||||||||||||||||||||||
▲ | gf000 4 days ago | parent | next [-] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
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. | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
▲ | masklinn 4 days ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
> 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. | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
▲ | torginus 4 days ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
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. | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
▲ | connicpu 3 days ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
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. | |||||||||||||||||||||||||||||||||||||||||||||||||||||
▲ | anonymoushn 4 days ago | parent | prev [-] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
defer { close all the files in the collection } ? | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|