Roland Kuhn
Typesafe – The software stack for applications that scale.
twitter: @rolandkuhn
On Tue, May 15, 2012 at 11:17 AM, Stefan Zeiger <sze...@novocode.com> wrote:On 2012-05-15 11:14, Roland Kuhn wrote:Adding map/flatMap/filter with right-bias would get us the best of bothworlds. If we want to be fancy we can include something like the following:object Try {def apply[T](body: => T): Either[Throwable, T] = try Right(body) catch{ case NonFatal(ex) => Left(ex) }}+1Try[T] could just be Either[Throwable, T]. You may want a right-biasedEither with other kinds of errors than Throwable (e.g. accumulating multipleerrors in a Seq[Throwable]).The semantics are very different: The combinators in Try catchexceptions, actively catering for non-total functions. [1]
Incidentally, the way it does this seems very dangerous: `catch e =>`is usually not the right thing; it catches Error, ControlThrowable.
Adding map/flatMap/filter with right-bias would get us the best of both worlds. If we want to be fancy we can include something like the following:
object Try {
def apply[T](body: => T): Either[Throwable, T] = try Right(body) catch { case NonFatal(ex) => Left(ex) }
}
this would be an easy way to enter it.
for {
v <- Try { computeValue }
if isOkay(v)
} yield transform(v)
Should this not be nice for everybody? No need to sacrifice the unbiased aspect for those who don’t want to use the newfangled stuff.
Regards,
Roland
May 15, 2012 kl. 11:06 skrev Heiko Seeberger:
> Why not simply right bias Either?
>
> Heiko
>
> On May 15, 2012, at 7:30 AM, Heather Miller wrote:
>
>> Hi all,
>>
>> We introduced `Try` a while back along with SIP-14 (Futures). `Try` is analogous to `Either` with the exception that it's success-biased. That is, Either is symmetric, it has two type parameters that are wrapped (symmetrically) in either `Left` or `Right`. In general, by convention, `Either` can be used to represent a "successful" or a "failed" result, by representing some generic type in its right projection, and a subtype of Throwable in its left projection. However, this is by convention only, one can of course swap these projections (whether accidentally or purposefully) or one can represent two successful values in each projection if they so desire.
>>
>> `Try`, on the other hand, is parameterized only on one type, with this success/failure use case in mind. That is, the successful case is wrapped in `Success` (parameterized on T) while the failed case is a `Throwable` wrapped in `Failure`.
>>
>> `Try` comes from Twitter, who use it frequently in their codebase (see twitter/util on github), and who have found that it results in more readable and less error-prone code for this success/failure use case of `Either`.
>>
>> Back then, we tentatively decided that it would go in package scala.util.
>>
>> However, this is a bit awkward because, as an analogue to `Either` (which lives in package scala and needs no import) `Try` lives in scala.util and requires the following import:
>>
>> import scala.util.{Try, Success, Failure}
>>
>> So- the question is, are people happy with it this way? There was all kinds of talk-- eventually moving `Either` to the same package/sub-package as `Try`, for example.
>>
>> Cheers,
>> Heather
>>
>> --
>> Heather Miller
>> Doctoral Assistant
>> EPFL, IC, LAMP
>> http://people.epfl.ch/heather.miller
>>
>
Roland Kuhn
Typesafe – The software stack for applications that scale.
twitter: @rolandkuhn
I'm with √iktor and Heiko.Right-bias Either.