https://kotlinlang.org logo
Title
m

Mohamed Ibrahim

07/09/2019, 1:05 PM
is it possible to create new keyword like in Kotlin for example I have a dyanmic string and want to map it to one of possible values .. so I think
when
is the right choice .. what I need to achieve something like this
when ( lang ) {
equalIgnoreCase "AR" -> "Arabic"
equalIgnoreCase "EN" -> "English"
equalIgnoreCase "RU" -> "Russian"
}
m

max

07/09/2019, 1:09 PM
when {
  lang.equals("AR", true) -> "Arabic"
}
w

wbertan

07/09/2019, 1:09 PM
Not sure, but you could use an extension? Something like
private fun String.expandName(): String =
            when {
                this.equals("AR", true) -> "Arabic"
                this.equals("EN", true) -> "English"
                this.equals("RU", true) -> "Russian"
                else -> this
            }

    @Test
    fun asas() {
        val value: String = "AR"
        assertEquals("Arabic", value.expandName())
    }
m

Mohamed Ibrahim

07/09/2019, 1:11 PM
I used this ``infix fun String?.equalIgnoreCase(other: String) = this.equals(other, true) ``
g

gildor

07/09/2019, 1:29 PM
when(lang.toUpperCase()) {
  "AR" -> "Arabic"
}
☝️ 4
r

Ryan Mentley

07/09/2019, 11:23 PM
lang.toUpperCase(<http://Locale.US|Locale.US>)
unless you enjoy surprising behavior in other locales 🙂
g

gildor

07/09/2019, 11:26 PM
Yes, or even better Locale.ROOT