On Tue, Oct 30, 2012 at 11:02 PM, Ricky Clarkson
<[email protected]> wrote:
> Let's say this is the original with pattern-matching:
>
> val s: String = option match {
> case None => getValue
> case Some(foo) => foo.asText
> }
>
> You could write this as:
>
> val s: String = option.map(_.asText).orElse(getValue)
>
> I might have misunderstood, it's just hit midnight and I've had a long
> day. If I have, can you give a code example? Tony Morris' cheat
> sheet might help; http://blog.tmorris.net/scalaoption-cheat-sheet/
Hmm... yeah, I'm not explaining this well.
The None case is treated the same as a case of a type I am not ready
to deal with. So, the code is:
val t = Option(page.querySelector(".fooClass")) match {
case Some(x:HtmlInputElement) => x.getValueAttribute()
case Some(x:DomNode) => x.asText()
case _ => ""
}
Typing it, it occurs to me I could probably just skip the Option
entirely (Right? That just drops the Some from the two cases I
define.). My gut reaction on any method that returns null on failure
is to immediately wrap it in Option() now, though.
--
You received this message because you are subscribed to the Google Groups "Java
Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/javaposse?hl=en.