Mark
05/24/2020, 3:37 AMoperator fun invoke(...): MyClass?
to return a nullable value, or is that confusing to the caller since it looks like a constructor call?Jakub Pi
05/24/2020, 4:51 AMKroppeb
05/24/2020, 10:25 AMMichael de Kaste
05/27/2020, 12:30 PMrequire
statement. However, that's not always what you want from it. What if I just want to construct class instances where my inputs are correct, disregarding the other input. You'll have to catch the exception and unbox it again just to filter them out.
I think in those cases I'd recommend the operator fun invoke constructors. An exception shouldn't be thrown in situations where you are aware of the input and output parameters and where returning null is acceptable.Jakub Pi
05/27/2020, 2:00 PM.from()
or .of()
in your companion object instead of operator invoke()
, especially in cases where you are doing an explicit conversion or if your type is an aggregate. You can also return an Option<MyClass>
or Either<SealedClassError, MyClass>
... Again these will require greater investment, but give your correspondingly more information and control. The invoke()
with null is the simplest solution; start there and move up the complexity chain based on your requirements.