Scala Option: map vs Pattern Matching

后端 未结 3 1308
耶瑟儿~
耶瑟儿~ 2020-12-29 09:35

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[MyC

相关标签:
3条回答
  • 2020-12-29 10:04

    I would go for this:

    myOptionInstance.fold(Nil: List[String])(...)
    
    0 讨论(0)
  • 2020-12-29 10:14

    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
    0 讨论(0)
  • 2020-12-29 10:15

    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)
    
    0 讨论(0)
提交回复
热议问题