https://kotlinlang.org logo
Title
j

Jérémy Touati

08/17/2019, 11:36 AM
quick question, do you people (especially those working with IntelliJ): - always specify types explicitly, - never specify them when IntelliJ can infer them (since it will show them as hint text instead, at least with the IDE's default options) - something in-between? (like, only for interface/abstract/open methods' return types since they define a contract) i suppose it's mostly a matter of preference in some cases, but i find myself hesitating a bit when writing super simple/closed methods that return a boolean, or when using things such as
List#toMutableList()
(it seems a bit overkill to mark the resulting variable as a
MutableList
then), so i ended up going for the 3rd approach (aka, only for overridable stuff), but i was curious if there was some kind of consensus on this
t

tseisel

08/17/2019, 11:49 AM
It's indeed a matter of preferences. There are some cases where specifying the type explicitly is advisable : 1. Exposing the return type of a function as another type (for example, returning a
MutableList
as a
List
) 2. Interface implementations 3. Public functions in general (in order not to break code that use them when changing the implementation)
4
j

Jérémy Touati

08/17/2019, 12:15 PM
fair points, thanks!