Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Option: map vs Pattern Matching

Tags:

scala

While dealing with Option in Scala what are the things I should be considering to decide whether to map or patten match? For example, if I have Option[MyClass], I can deal with it the following ways:

def getList(myOptionInstance: Option[MyClass]): List[String] =
  myOptionInstance map (...) getOrElse(List.empty[String])

or

def getList(myOptionInstance: Option[MyClass]): List[String] = myOptionInstance match {
  case Some(mySomeInstance) => .....
  case None => List.empty[String]
}

When will I choose one over the other?

like image 549
Prasanna Avatar asked Feb 21 '14 01:02

Prasanna


People also ask

Does Scala have pattern matching?

Notes. Scala's pattern matching statement is most useful for matching on algebraic types expressed via case classes. Scala also allows the definition of patterns independently of case classes, using unapply methods in extractor objects.

What is the use of Scala pattern matching?

Pattern matching is a way of checking the given sequence of tokens for the presence of the specific pattern. It is the most widely used feature in Scala. It is a technique for checking a value against a pattern. It is similar to the switch statement of Java and C.

How do I use options in Scala?

The Option in Scala is referred to a carrier of single or no element for a stated type. When a method returns a value which can even be null then Option is utilized i.e, the method defined returns an instance of an Option, in place of returning a single object or a null.

What is some function in Scala?

Scala some class returns some value if the object is not null, it is the child class of option. Basically, the option is a data structure which means it can return some value or None. The option has two cases with it, None and Some.


2 Answers

I second @rarry: fold is the preferred way to deal with this.

Some prefer pattern matching because it's "cool" (whatever it means) and sometimes easier to read.

I try to avoid using getOrElse because it does not force you to use the same type for the default value as the type wrapped in your Option:

def getOrElse[B >: A](default: ⇒ B): B

So you can write:

val v = Some(42).getOrElse("FortyTwo")

Here v has type Any. It's very easy to see the problem with such a stupid example but sometimes it's not as obvious and can lead to issues.

While fold:

def fold[B](ifEmpty: ⇒ B)(f: (A) ⇒ B): B

It forces you to return the same type for both branches.

scala> Some(42).fold("fortyTwo")(v => v)
<console>:8: error: type mismatch;
 found   : Int
 required: String
              Some(42).fold("fortyTwo")(v => v)
like image 60
vptheron Avatar answered Oct 06 '22 07:10

vptheron


Pattern matching is :

  • slightly more efficient
  • not anal about subtypes (in this case @rarry had to add a type hint)
  • easier to read
  • endorsed by Martin Oderksy: https://stackoverflow.com/a/5332657/578101
like image 39
mauhiz Avatar answered Oct 06 '22 07:10

mauhiz