Okay I know you can have sealed classes for a when...
# announcements
w
Okay I know you can have sealed classes for a when check to verify that you are covering all branches. is the reverse possible say given a string input I output all sealed classes under a hirachy? I'm guessing no the type checker can't verify
g
what is the meaning behind "string input"? There is
::class.sealedSubclasses
if that's what are you looking for
h
Ha! Hesitated to ask that question Yesterday xD my usecase was as Well that i got object input and wanted to verify that if the given input CAN be parsed it will be parsed correctly.
w
Copy code
return when(type) {
                CostPerTransaction::class.simpleName -> CostPerTransaction(unit())
                CostPerTransactionTiered::class.simpleName -> CostPerTransactionTiered(unit(), upper())
                PercentageFee::class.simpleName -> PercentageFee(percent())
                PercentageFeeTiered::class.simpleName -> PercentageFeeTiered(percent(), upper())
                ...
Thats what I mean by string the variable type is a string. The classes "PercentageFee" etc are in a sealed hirachy. Basically I would like if someone adds to the hirachy to know to find this piece of code and add to it. i.e. all possible branches are covered.
s
Kotlin has exhaustiveness check for enums and sealed classes only. Is it possible to solve this with enum?
w
I was wondering that myself.