Option and Either conversion to Try

1,377 views
Skip to first unread message

Haney Maxwell

unread,
Oct 28, 2013, 12:50:22 AM10/28/13
to scala...@googlegroups.com
Hi,

It looks like Option has toRight and toLeft, and LeftProjection and RightProjection have toOption, and Try has toOption, but Option => Try, Either => Try, and Try => Either aren't there. Is there a reason there isn't something like:
"
def toSuccess[X <: Throwable](failure: => X) =
  if (isEmpty) Failure(failure) else Success(this.get)
def toFailure[X](success: => X)(implicit ev: A <:< Throwable) =
  if (isEmpty) Success(success) else Failure(this.get)
"
in Option?

Thanks,

Haney Maxwell


atomly

unread,
Oct 28, 2013, 12:17:39 PM10/28/13
to Haney Maxwell, scala-user
can't you just do something like?


  opt.map(Success(_)).getOrElse(Failure(new Throwable("oops")))

or

  Try { opt.getOrElse(throw new Throwable("oops")) }

:: atomly ::

[ ato...@atomly.com : www.atomly.com  : http://blog.atomly.com/ ...
[ atomiq records : new york city : +1.347.692.8661 ...
[ e-mail atomly-new...@atomly.com for atomly info and updates ...


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

vatel

unread,
Oct 29, 2013, 1:05:24 AM10/29/13
to scala...@googlegroups.com
Or even: Try { opt.get }

if the exception type is not important

--
Vatel

Brendan Higgins

unread,
Oct 30, 2013, 4:12:31 AM10/30/13
to scala...@googlegroups.com, hm...@uchicago.edu
I am actually also curious. Similiarly, Option is viewable as an Iterable since a None type can be thought of as empty and a Some type can be thought of as a singleton. So, as a matter of consistency and convenience, why does the standard library not do this for us?

pagoda_5b

unread,
Oct 31, 2013, 5:31:56 AM10/31/13
to scala...@googlegroups.com, hm...@uchicago.edu

pagoda_5b

unread,
Oct 31, 2013, 5:33:50 AM10/31/13
to scala...@googlegroups.com, hm...@uchicago.edu
Sorry, I suspect that I've misread the question...
you meant why the standard library doesn't do it for Try?

Ivano

Haney Maxwell

unread,
Oct 31, 2013, 12:32:51 PM10/31/13
to pagoda_5b, scala...@googlegroups.com
Yeah, that was the question.

Sonnenschein

unread,
Nov 1, 2013, 5:08:52 AM11/1/13
to scala...@googlegroups.com, pagoda_5b, hm...@uchicago.edu
Reply all
Reply to author
Forward
0 new messages