Binary operator with Option arguments

前端 未结 5 995
萌比男神i
萌比男神i 2021-02-09 07:12

In scala, how do I define addition over two Option arguments? Just to be specific, let\'s say they\'re wrappers for Int types (I\'m actually working with maps of do

5条回答
  •  梦毁少年i
    2021-02-09 07:34

    If they both default to 0 you don't need pattern matching:

      def addOpt(a:Option[Int], b:Option[Int]) = {
        a.getOrElse(0) + b.getOrElse(0)
      }
    

提交回复
热议问题