Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 161 Vote(s) - 3.61 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why Kotlin compiler requires the explicit initializer of var property?

#1
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?
Reply

#2
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:

[To see links please register here]

.

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
Reply

#3
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")
}
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through