if I’m doing: ```when (string){ "SomeString"...
# getting-started
o
if I’m doing:
Copy code
when (string){

    "SomeString" -> { }
}
can i have it somewhere set to ignore case when matching?
🚫 1
hmm it doesn’t really make sense to do that though cause it defeats the purpose
n
Try something like this
Copy code
with("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")
      }
    }
o
never knew you could have functions there
thanks
🤝 1
s
Why not when (string.lowercase()))
👍 2
d
Copy code
when(string.toLower()) {
 ... 
}
Might be the better option when all patterns should ignore the case
o
the incoming string is always going to be lower case, and the thing being matched will be this enum class
Copy code
enum 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
then i could do
enum.name.toLower()
I guess?
s
Yes (lowercase or toLowerCase which is deprecated)
n
@oday yes like this
enum.name.lowercase()
d
depending on your use case, you might want to do sth like this
Copy code
enum 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 -> ...
    }
}
The main advantage is, that adding a new value to your enum will lead to a compile time error if you forget to add the new value to the when block
o
that’s really good actually
yea I didnt think further than the current case, and indeed there will be other values
ok so I’ve implemented this and a strange thing has happened, there was an
else
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 redundant
should be
.find()
not
.first()
cause of this case that i just mentioned
jep, that did it
s
in the spirit of "don't repeat yourself", let's remove the value from the enum.
Copy code
enum 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-48872
o
ok yes that is nice and goes the other route of making the value upper case, so as not to repeat, very nice thank you