Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Kotlin compiler requires the explicit initializer of var property?

Tags:

kotlin

I can't understand the following piece of Kotlin documentation:

The initializer, getter and setter are optional. Property type is optional
if it can be inferred from the initializer or from the base class member being overridden.

Examples:
var allByDefault: Int? // error: explicit initializer required, default 
getter and setter implied

The only explanation of why the compiler requires explicit initializer here (at least the only explanation I can come up with) is that Kotlin does not have default values of properties. Is it right? If so, why? In other words: what is the difference between Kotlin properties and Java fields (which have default values) which doesn't allow us to have default values of properties?

like image 935
aga Avatar asked Nov 14 '15 18:11

aga


2 Answers

That's simple: in Java default values are 0 (zero) and null. But in Kotlin most of the values are not-nullable, so you can't initialise them with null. For primitive values there could be a default strategy of initialising with zeros, but it was not done in order to be consistent. But in primitive arrays the default value is zero indeed.

If you really need that initialisation semantic, take a look at lateinit properties: https://kotlinlang.org/docs/reference/properties.html#late-initialized-properties.

That mechanism basically allows to init a field with null, but then frees you from null assertions.

ADDITION

Actually Kotlin is very clever about initialization. For example that works:

val x: Int

if(something)
    x = 1
else
    x = 2

println(x)

Here kotlinc may proove that x is being initialized before it is being used, so the code is OK

like image 116
voddan Avatar answered Oct 16 '22 14:10

voddan


Kotlin does not do anything implicitly. It does not convert numeric types without your specific instruction, nor does it set a default or initializing value without it being explicit. It is a design choice to eliminate common errors that were found in typical Java programs. It isn't clear to the compiler if you forgot to initialize it or if you meant for a default value to be used. Since it isn't clear, it is bad. And therefore likely results in bugs.

The design choices of Kotlin help eliminate bugs due to code in which the compiler cannot help determine if there is an error. It is philosophical and consistent in the language.

Kotlin requires initialization before use. For members that means by the time constructors and initializers are completed, it must have a value. lateinit modifier on var allows this to be ignored at compile time although at runtime the check is done when you access the variable. For local variables, any branch of code must initialize the value before access. For example:

fun stateFromAbbreviation(abbreviation: String?): String {
    val state: String

    if (abbreviation == null) {
        state = DEFAULT_STATE
    }
    else {
        state = stateMap.get(abbreviation) ?: throw IllegalStateException("Invalid state abbreviation $abbreviation")
    }

    return state
}

Here the local variable can be initialized in the if statement assuming all branches initialize the value. But really, this code would be more idiomatic using the if as an expression, such as:

fun stateFromAbbreviation(abbreviation: String?): String {
    return if (abbreviation == null) {
        DEFAULT_STATE
    }
    else {
        stateMap.get(abbreviation) ?: throw IllegalStateException("Invalid state abbreviation $abbreviation")
    }
}
like image 38
2 revs Avatar answered Oct 16 '22 12:10

2 revs