So, currently, there is no option for if I want to...
# getting-started
g
So, currently, there is no option for if I want to define that an Interface method should return either one class or some other, right? Type composition like:
Copy code
typealias SourceOrFactory<T> = List<T> | DataSource.Factory<T>
c
I don’t think so. Think you would have to do use a generic container and use something like
when
to check the type
g
Doesn't sound like a great approach. The kind of compile-time check for that isn't really good if even possible.
l
Maybe you can use sealed classes
a
how would the compile-time checks look like for your example?
g
For example, if a method was to return
SourceOrFactory<T>
, if I tried to override it returning something that wasn't either
List<T>
or
DataSource.Factory<T>
it could be caught as an error.
It is as simple as any compile-time type checking, only difference being it would match for more than one type.
a
the closest you can get there is by using a
sealed class
g
Yes, indeed. Not as good as I will need to define wrappers for the classes, but works.
k
This is also implemented in #arrow by the
Either
class.
g
Would you believe if I told you I was looking at it right now?
Actually, was reading about Validated and Ior currently
Not enough to make me want to add a dependency, but Arrow seems very interesting overall
k
Haha that's exactly my feeling about it.
m
What would be a good practice use case for this "Either" behavior? Intuitively sounds like an anti pattern to me..
k
Error handling without exceptions:
Either<Result, Error>
m
Ok yeah that makes sense, thanks
k
You could say nullability is an example of this too,
T? == Either<T, Unit>