Is it possible to test a String for not being equal in a when-statement?
This is of course perfectly possible with a simpel if statement:
val storedValue = sharedPreferences.getString(identifier, NOT_SET)
if (storedValue != NOT_SET) {
super.setValue(storedValue)
}
However, I like how storedValue is scoped inside of the when-statement in this snippet:
when (val storedValue = sharedPreferences.getString(identifier, NOT_SET)) {
NOT_SET -> {}
else -> super.setValue(storedValue)
}
The downfall is the empty code block for the NOT_SET entry.
Is it possible to combine these two?
I'd like to scope storedValue and get rid of empty code blocks. The result would be comparable to:
when (val storedValue = sharedPreferences.getString(identifier, NOT_SET)) {
!NOT_SET -> super.setValue(storedValue)
}
Since SharedPreferences is part of the Android framework, this would another solution:
if (sharedPreferences.contains(identifier)) {
super.setValue(sharedPreferences.getString(identifier, null))
}
However, the goal of my question is deeper understanding of the possibilities of Kotlin, for the sake of learning.