https://kotlinlang.org logo
Title
e

evkaky

02/07/2018, 5:55 PM
Hi guys. Can I obtain default value for generic type T? I need something similar to
T variable = default(T)
in the C# My first try in kotlin:
data class Node<K>(val key: K)

class Container<K> {
   var head = Node<K>() // err here, I have to specify *key* prop in the node ctor, but how?
}
What can I use?
a

Andreas Sinz

02/07/2018, 10:53 PM
@evkaky the difference between kotlin and C# is, C# has implicit default values and kotlin does not. In C#
int x;
is fine, but in kotlin you have to intialize everything before using it, makes it obvious to the reader what value
x
has
usually Kotlin tries to promote a coding style that makes "forgetting" to set stuff harder
e

evkaky

02/08/2018, 9:36 AM
you are right