by br1 · 8y ago · 0 comments
func f() {
x := ...
if x.something() {
x.doSomethingEarlier()
defer x.cleanup()
}
// use x however you like
}
where scope-based forces you to do stuff like func f() {
x := ...
if x.something() {
x.doSomethingEarlier()
defer x.cleanup()
// use x however you like
} else {
// use x however you like
}
}
In a scope-based defer, you'd have to keep all related code in the scope, nesting it another layer deeper / possibly duplicating it.On the flip-side is of course that this doesn't work like most would probably want in function-scoped:
for i := 0; i < 4; i++ {
x := get(i)
defer x.cleanup()
x.whatever()
}
and you're forced to for i := 0; i < 4; i++ {
x := get(i)
func() {
defer x.cleanup()
x.whatever()
}()
}
I've seen both of these patterns pretty frequently, in Go and in other languages. Go could, of course, have both a func_defer and a scope_defer, but that doesn't seem like it'd fit with the fairly strong focus on keeping the language feeling small and simple. So they had to pick one, and it can't handle both cases.I mean, you can convert them into each other. Scoped can do something like this (go+python blended code 'cuz lazy):
func f(){
deferred := []
defer func() { for d in deferred.reverse(): d() }() // plus error handling
if x.something() {
deferred.push(func(){ cleanup() });
}
// same as func scope
}
but that's a bit more ridiculous / error-prone (though a helper func is obviously possible) than the equivalent IIFE for func -> scope. More explicit, I suppose, but bleh.That's clearly false. You could set up a list to hold objects to be disposed (or, more generally, closures to execute) and defer a simple procedure that disposes of all objects in the list. This is in fact what the implementation of defer must do internally.
So what I really wish was that languages used dot notation to go up scope eg ..name is that name two blocks out. This was one of the good parts of VB syntax which I miss in other languages. Think how much nicer it is than python's nonlocal and global, for example!
func someSQLStuff() {
tx, err := createTx()
defer func() {
if err != nil {
tx.Rollback()
log(err)
} else {
tx.Commit()
}
}()
rows, err = tx.QueryContext( ... )
// more SQL
}
Basically, function-scoped cleanup. Like closing opened files. func f(i interface{}) {
if closable, ok := i.(closable); ok {
defer closable.close()
}
// do stuff with i, maybe other casts, etc
}
There aren't many nice options for "if I can call X, defer a call to X" aside from shoving it into an `if`, where it'd be captured by that scope. I mean, you could do something like deferrable := func(){}
if closable {
deferrable = closable.close
}
defer deferrable()
but imagine doing that every time. It'd work, sure, but it'd also be more annoying. func closeIfNecessary(object interface{}) {
closable, needsClosing := object.(closable)
if needsClosing {
closable.close()
}
}
And then just do: func f(i interface{}) {
defer closeIfNecessary(i)
...
}
Doing it this way also saves you boilerplate by factoring the downcast out into a separate function.I would love to be able to explicitly affect other scopes as you mention, for example to define two classes at the same time.