In Kotlin, specifying the return types of functions and the types of variables is usually optional but can be optionally included.
I've encountered a few benefits of including them:
- It sometimes makes type errors more understandable as when types don't match it helps the compiler determine which is wrong and which is right.
- It makes the code more explicit and therefore more clear
- It might improve build times. I haven't found a definitive answer on how big an effect it has though.
On the other hand, there are a couple of benefits of not including type declarations:
- Less typing
- More concise code
I know the Kotlin coding conventions say to include them in the interface of a library, which makes sense but doesn't apply to most of my code.
I believe in the Haskell community where type declarations are also optional, it's considered a good idea to always explicitly declare the types of functions.
Is there any kind of well regarded convention on when we should include them in Kotlin?