is it possible to use extension functions in `when...
# announcements
a
is it possible to use extension functions in
when
statements?
Copy code
when(myObject) {
  myCustomExtensionFunctionOnMyObject -> do something
}
m
I believe you would have to use
when
expression without a subject (assuming the extension returns boolean)
Copy code
val myObject = ...

when {
myObject.myCustomExtensionFunctionOnMyObject() -> do something
}
a
hmm, then multiple cases do not combine
like
Copy code
when {
  myObject.check1() -> do1
  myObject.check2(),
  myObject.check3() -> do23
  else -> doElse
}
Maybe && will work
or ||
👍 1
m
yes it will, but I think you meant ||
j
you can also define a val in when:
Copy code
when (val myObj = someProp.getSth()) {
    myObj.test1() -> //...
    myObj.test2() -> //...
}
might be handy for some use cases
👍 1
he wants to use it with a when
d
Silly me, thanks @jaqxues