diesieben07
05/24/2017, 9:31 PMif (item.p == null) { } else { /* item.p is known to be not null here */ }
should work then.diesieben07
05/24/2017, 9:31 PMitem.p
as if it were non-nulldiesieben07
05/24/2017, 9:31 PMRuckus
05/24/2017, 9:32 PMRuckus
05/24/2017, 9:32 PMitem
and item.p
are valsjuliocbcotta
05/24/2017, 9:34 PMjuliocbcotta
05/24/2017, 9:34 PMjuliocbcotta
05/24/2017, 9:34 PMjuliocbcotta
05/24/2017, 9:39 PM?
and we want to use it instead of if(item.p == null)
.. it is less verbose… but when we need the else
it is not so intuitive the current option.kingsley
05/24/2017, 9:40 PMitem.p?.let {
println("executed if not null")
} ?: run {
println("executed if null")
}
diesieben07
05/24/2017, 9:41 PM?.
is mostly useful if you are interested in the return value of something. E.g. val myValue = thing?.value ?: defaultValue
- which is very much less verbose.diesieben07
05/24/2017, 9:42 PMmg6maciej
05/24/2017, 9:42 PMif
statement is something I never liked, but I quite enjoy if
expression in Kotlin. I barely use ?. ?:
.kingsley
05/24/2017, 9:45 PM?. ?:
isn't the nicest piece of code. I only fixed the original code posted by Julio. However, this also has it's own pros 🙂diesieben07
05/24/2017, 9:46 PM?.
and ?:
are great, but only if you have simple values. if you need to calculate an expression in the branches if-else is nicerdiesieben07
05/24/2017, 9:47 PMtrevjones
05/24/2017, 9:50 PMwhen
would be my goto at that pointRuckus
05/24/2017, 9:50 PMif
. Only
when (condition) {
true -> { ... }
false -> { ... }
}
🧌trevjones
05/24/2017, 9:50 PMcodeslubber
05/25/2017, 1:35 AMjw
05/25/2017, 1:47 AMcodeslubber
05/25/2017, 1:52 AMcodeslubber
05/25/2017, 2:22 AMjw
05/25/2017, 2:59 AMkkozmic
05/25/2017, 6:25 AMFoo<T>
? It obviously cannot be reified...myanmarking
05/25/2017, 8:50 AMmarcinmoskala
05/25/2017, 12:44 PMmarcinmoskala
05/25/2017, 12:44 PMwjur
05/25/2017, 1:16 PMalexfu
05/25/2017, 1:18 PMclass Person(val state: State = State.SINGLE) {
val State.isSingle: Boolean
get() = this == State.SINGLE
enum class State {
SINGLE, MARRIED
}
}
val john = Person()
print(john.state.isSingle) // Unresolved reference: isSingle