vinny2020
09/01/2020, 4:07 AMRuckus
09/01/2020, 4:11 AMinit
block
• Use a helper function with a nullable/result wrapper returnvinny2020
09/01/2020, 4:16 AMthanksforallthefish
09/01/2020, 5:32 AMstreetsofboston
09/01/2020, 11:30 AMinvoke
on a class' companion object):
class MyClass {
...
companion object {
operator fun invoke(value: Int, ..., ...): MyClass? {
if (value < 0) return null // return null instead of throwing exception
...
return myClass
}
}
}
kqr
09/02/2020, 7:13 AMstreetsofboston
09/02/2020, 11:43 AMnull
or an Optional<MyClass>
with the value None
or an Either.Left
value if input validation fails? 😀
Smart-constructors (basically a factory that looks like a constructor when you call it) allow you to do this.kqr
09/02/2020, 11:58 AMstreetsofboston
09/02/2020, 12:41 PM