oday
01/02/2023, 12:38 PMwhen (string){
"SomeString" -> { }
}
can i have it somewhere set to ignore case when matching?oday
01/02/2023, 12:38 PMNilesh Rathod
01/02/2023, 12:47 PMwith("string") {
when {
equals("SomeString", ignoreCase = true) -> println("position is 1")
contains("SomeString", ignoreCase = true) -> println("foo")
startsWith("SomeString", ignoreCase = true) -> println("bar")
else -> println("SomeString")
}
}
oday
01/02/2023, 12:51 PModay
01/02/2023, 12:51 PMSaku Ytti
01/02/2023, 1:04 PMdf
01/02/2023, 1:04 PMwhen(string.toLower()) {
...
}
Might be the better option when all patterns should ignore the caseoday
01/02/2023, 1:07 PMenum class Options(val value: String) {
--> NEARBY("nearby"),
--> ANYWHERE("anywhere")
}
}
i’m having to give each one a name so that this matching could makes sense
what I wanted to do is leave the enums without a name and just match the enum by its .name
oday
01/02/2023, 1:07 PMenum.name.toLower()
I guess?Saku Ytti
01/02/2023, 1:08 PMNilesh Rathod
01/02/2023, 1:11 PMenum.name.lowercase()
df
01/02/2023, 1:20 PMenum class Option(val value: String) {
NEARBY("nearby"),
ANYWHERE("anywhere");
companion object {
fun fromValue(value: String) = Option.values().first { it.value == value }
}
}
fun main() {
val enumValue = Option.fromValue("nearby")
when (enumValue) {
NEARBY -> ...
ANYWHERE -> ...
}
}
df
01/02/2023, 1:23 PModay
01/02/2023, 1:26 PModay
01/02/2023, 1:26 PModay
01/02/2023, 1:37 PMelse
branch inside the when that I rely on to know whether the value is not part of the enum defined, when it isn’t part of the enum defined, it’s going to be a cityId, and then I do something with that
the way it’s become now, all cases are covered and the else has become redundantoday
01/02/2023, 1:38 PM.find()
not .first()
oday
01/02/2023, 1:39 PModay
01/02/2023, 1:41 PMStephan Schröder
01/04/2023, 10:03 AMenum class Option {
NEARBY,
ANYWHERE;
companion object {
fun fromAnyCase(value: String) = with(value.uppercase()) {Option.values().find { it.name == this }}
}
}
also maybe add a TODO-comment to replace Option.values()
to Option.entries
in Kotlin 1.9. Apparently Enum.values()
isn't the most efficient thing: https://youtrack.jetbrains.com/issue/KT-48872oday
01/04/2023, 12:03 PM