Binary operator with Option arguments

前端 未结 5 841
渐次进展
渐次进展 2021-02-09 06:49

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条回答
  •  时光说笑
    2021-02-09 07:33

    (Repeating comment above in an answer as requested)

    You don't extract the content of the option the proper way. When you match with case Some(x), x is the value inside the option(type Int) and you don't call get on that. Just do

    case Some(x) => x 
    

    Anyway, if you want content or default, a.getOrElse(0) is more convenient

提交回复
热议问题