Shawn Karber_
06/11/2020, 10:45 PMdef is_ten(x) when x > 10, do: "Greater than ten"
def is_ten(x) when x < 10, do: "Less than ten"
I need to write a kotlin function that always takes a string, but the logic is completely different based on that string, is there a way to encapsulate logic into a function that is called based on the parameter value similar to elixir guard clauses?nkiesel
06/11/2020, 11:11 PMfun is_ten(x: String) = if (x == "10") do_ten(x) else do_other(x)
but I still don't get what it is you are looking for.Shawn Karber_
06/11/2020, 11:16 PMfun runDebug(type: String) : Unit {
if (type == "Test") {
doA()
} else if (type == "Debug") {
doB()
} else {
doC()
}
}
That feels really hacky, so I was wondering if something like guard clauses exist in kotlinShawn Karber_
06/11/2020, 11:17 PMfun runDebug(when type == "Test") {
}
fun runDebug(when type == "Debug") {
}
jw
06/11/2020, 11:47 PMfun runDebug(type: String) {
fun test() {
// test code
}
fun debug() {
// debug code
}
fun mock() {
// mock code
}
switch (type) {
"test" -> test()
"debug" -> debug()
"mock" -> mock()
else -> error("Unknown $type")
}
}
maybe like this?jw
06/11/2020, 11:48 PMfun runDebug(type: String) {
switch (type) {
"test" -> {
// test code
}
// etc.
}
}
thoughjw
06/11/2020, 11:48 PMfun runDebug(type: String) = switch (type) {
"test" -> {
// test code...
}
// etc.
}
Shawn Karber_
06/11/2020, 11:48 PM