Srki Rakic
02/01/2022, 12:44 AMValidated
instead require
for validation when constructing an object.
class Probability private constructor(val value: Double) {
companion object {
fun create(value: Double) =
if (value > 0.00 && value < 1.00) Probability(value).valid()
else ValidationError.ProbabilityOutOfBounds("Probability must be between 0.00 and 1.00").invalid()
}
}
I'm struggling to find the best way to structure it in a given context. Typical use case for `Probability`:
val fsmIdentifiers =
listOf(
FsmIdentifier(
key = "first",
mProbability = Probability(0.95),
uProbability = Probability(0.1),
compare = ::jaroWinklerDistance
),
FsmIdentifier(
key = "last",
mProbability = Probability(0.99),
uProbability = Probability(0.01),
compare = ::jaroWinklerDistance
),
FsmIdentifier(
key = "middle",
mProbability = Probability(0.90),
uProbability = Probability(0.1),
compare = ::jaroWinklerDistance
),
......
)
As you see, the Probability
is created as part of the FsmIdentifier
(and other similar identifiers). Any suggestion on how to handle the creation with Validated
in this context cleanly, without making the code look overwhelmingly verbose?stojan
02/01/2022, 9:45 AMSrki Rakic
02/01/2022, 11:56 AM