val list: List<String> = listOf()
val list = listOf<String>()
val executor: Executor = mock()
val executor = mock<Executor>()
Which variant is “more” idiomatic? The type / type argument before or after the equal sign? Both variants are semantically equal. Which one do you like more?
Edit: Kotlin in Action 2nd 11.1 Creating types with type arguments: Generic type parameters only states
These declarations are equivalent.
val readers: MutableList<String> = mutableListOf()
val readers = mutableListOf<String>()
Personally for me, I prefer the type after the equals. If I write val myList: List<String> = listOf(), I’ve now specified the List type twice; both in the variable type, and by using listOf(). So I would write val myList = listOf<String>(). My goal is always to write the least amount of code possible while still being easy to read.
If you are developing libraries, it is recommended to explicitly declare types for all public properties. If it is private or you are using it in an application, you can use it however you like most. Both cases are readable.
like with most things it depends,
sometimes i do not define the type because the name of the variable implies it clearly
sometimes i make a type alias because the actual type is a generics sandwitch…
whatever you feel reads better and leaves no ambiguity for someone new to the code.
however i never do indentation like that just to make = at the same level… mostly because the formatter will get rid of it right away. Or it will annoy the next developer who wants to commit his stuff but formatter fixes your “bootiful” identation…
good code is the code that you spend least amount of time reading to make sense of… best code is no code