AFAIK, Iterator.map
is lazy while Vector.map
is eager, basically because they are different types of monads.
I would like to know if there is any chance of having a EagerTry
and LazyTry
that behave just like the current Try
, but with the latter (LazyTry
) delaying the execution of the closure passed until the result is needed (if it is needed).
Please note that declaring stuff as lazy
doesn't work quite well in Scala, in particular it works for a given scope. An alternative exists when passing parameters (parameters by name). The question is how to achieve lazy behaviour when returning (lazy) values to an outer scope. Option
is basically a collection of length 0 or 1, this would be an equivalent case for lazy collections (Iterator
, Sequence
) but limited to length 0 or 1 (like Option
and Either
). I'm particularly interested in Try
, i.e. using LazyTry
exactly as Try
would be used. I guess this should be analogous in other cases (Option
and Either
).
Please note that we already have EagerTry
, as the current standard Try
is eager. Unfortunately, the class is sealed, therefore, to have eager and lazy versions of the same class we would need to define the three of them and implement two of them (as opposed to defining and implementing one). The point is returning a Try
without other software layers worrying about the time of execution of that code, i.e. abstraction.