2 ms·
What would the goal be; for the compiler to warn if a type requires a method to be called on defer? What if you want to let the caller of the caller handle that
by bigdubs 6y ago
What would the goal be; for the compiler to warn if a type requires a method to be called on defer? What if you want to let the caller of the caller handle that? You would then need pragmas to silence the warning, which is something that the authors specifically don't want.
- Nullabillity 6y agoThe Rust answer would be that you implement `Drop` for your type, rather than asking you nicely to remember to close everything properly.
- bigdubs 6y agoCloser is an interface in go that you can test for, the issue here is interfaces are implicit in go.
- morelisp 6y ago> for the compiler to warn if a type requires a method to be called on defer? This seems a bigger ask than what's upthread and than what's necessary in most cases. You just need to make sure the return value is not implicitly discarded. Go linters already do this for `error`. I have used both __checkReturn and __attribute__ ((warn_unused_result)) in C projects before and both were helpful, easy to reason about when the warning occurred, easy to work-around in the rare cases I really truly did want to drop the result (in Go `_ = ...` is already idiomatic), and useful in semantically varied cases (resource allocation, error handling, pure functions).