dave08
02/24/2020, 6:15 PM// 1
data class SomeResult(val typeOne: Bundle? = null, val orTypeTwo: Intent? = null)
if (typeOne == null)
doSomething(typeTwo)
else
doSomething(typeOne!!)
or:
// 2
sealed class SomeResult {
class TypeOneWrapper(val result: Bundle) : SomeResult
class TypeTwoWrapper(val result: Intent) : SomeResult
}
when (result) {
is TypeOneWrapper -> doSomething(result)
is TypeTwoWrapper -> doSomething(result)
}
The second seems cleaner, but requires funny ...Wrapper and val names (since the result type is not owned by me...), but the first is not as robust (doesn't ensure that at least one is set...).Zach Klippenstein (he/him) [MOD]
02/24/2020, 6:17 PMResult
type: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-result/index.htmldave08
02/24/2020, 6:19 PMdave08
02/24/2020, 6:22 PMZach Klippenstein (he/him) [MOD]
02/24/2020, 6:30 PMResult
happens to represent – you could use the pattern for the union of any types.dave08
02/24/2020, 6:35 PMAny?
as the type and do all the checks inside... which is a bit like (1) usage wise, but hidden in the class...Kroppeb
02/24/2020, 8:41 PMdave08
02/24/2020, 9:00 PMfun Bundle.asAccountResponse(): Account
and fun Bundle.asRegistrationIntent():Intent
(which is my real use case).... but then how do you validate what was returned?Kroppeb
02/24/2020, 9:18 PMjimn
03/03/2020, 7:55 AM(result as? Type1)?.let{...}?:(result as? Type2)?.let{...}?: ...
is under represented for simple expressions. but abstract SomeResult.doSomething()
seems like the least astonishing thing a maintainer might read.