|
|
|
|
|
by Groxx
3096 days ago
|
|
I have a comment at a higher level with a broader example, but for Go at least this is somewhat common: 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. |
|