• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Example of using Either

 
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does anyone have any good examples of using Either -- that aren't covered by "Option" or "Try"?

 
Scott Shipp
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've made a guessing game example, where when you guess you can Either get a Prize indicating you guessed correctly or a GuessAgain that can tell you how close you got... but I'm looking for other good examples. Here is the gist at github: https://gist.github.com/scottashipp/783c74f68cea2f2e338d
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used Either in the past mainly for error handling. Very recently I used Either for processing and mapping collections!
 
Bartender
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alvin Alexander has a blog post discussing Either, although his simple example is similar in principle to yours. He suggests that Try/Success/Failure may be a better approach. With the language developing so fast, it's not always easy to work out what the current best practice might be.

I must admit I find all these, er, Options kind of confusing sometimes. I'm working on a Play prototype with ReactiveMongo and finding it difficult to know when/how to get the actual darned value out of a Future Option of Some Promise of Trying to yield Either a Success or a Failure... it sometimes feels more like Philosophy 101 than programming!

Still, at least it's been a while since I saw a NPE. Maybe I should create an Option of Some(NPE) just for old time's sake?
 
Scott Shipp
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joe Harry wrote:I used Either in the past mainly for error handling. Very recently I used Either for processing and mapping collections!



Awesome! I'd love to hear more, maybe see some example code?
 
Scott Shipp
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

chris webster wrote:I must admit I find all these, er, Options kind of confusing sometimes. I'm working on a Play prototype with ReactiveMongo and finding it difficult to know when/how to get the actual darned value out of a Future Option of Some Promise of Trying to yield Either a Success or a Failure... it sometimes feels more like Philosophy 101 than programming!



I hear ya! We have seen a couple instances where it makes sense to have a Try[Option[Something]]. One is calling a web service (we wrap the call result in a Try to indicate if the call succeeded or failed) then we wrap the successful response in an Option, indicating that the thing we're querying for may not be there. But still...that is probably the clearest it gets. I wouldn't go any further levels deep!

chris webster wrote:Still, at least it's been a while since I saw a NPE. Maybe I should create an Option of Some(NPE) just for old time's sake?



:-D

This is another topic and I'm sure I'm not telling you anything new but you can end up with Some(null) pretty easily, leading to an NPE. I've seen this directly happen using Some and passing a null value or by mapping out some hierarchy of objects and ending up with a null along the way. The first is the reason why best practice is to use "Option()" instead of "Some()" for creating an option.

Example #1 - REPL output of Some(null) causing an NPE


(^^^ That is fixed if you use "Option" everywhere that it is currently using "Some"..see below)

Example #2 - REPL Output of mapping out a hierarchy of objects where one value can return null


One way to get around the second example is to flatMap the value and explicitly wrap in option:

 
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic