Type inference for function return types

A bit off topic: While I personally prefer return types to be inferred when not stated explicitly, I recommend to always add explicit return types, except for trivial functions:

  • Documentation (similar to function arguments)
  • This checks that a function really returns what the writer intended it to return
  • If not given, and the implementation is wrong, it can make it hard to spot the root cause of a problem: write_int(to_int(...)) “Can’t pass str to write_int” vs. “Can’t return str from to_int”. (Easy to spot the problem in this toy example, but much harder in some real world code.)
1 Like