Partial Functions in Scala

前端 未结 3 1520
悲哀的现实
悲哀的现实 2021-02-20 07:19

I just wanted to clarify something about partially defined functions in Scala. I looked at the docs and it said the type of a partial function is PartialFunction[A,B]

3条回答
  •  长情又很酷
    2021-02-20 08:21

    If you are looking to set up a partial function that, in effect, takes multiple parameters, define the partial function over a tuple of the parameters you'll be feeding into it, eg:

    val multiArgPartial: PartialFunction[(String, Long, Foo), Int] = {
      case ("OK", _, Foo("bar", _)) => 0 // Use underscore to accept any value for a given parameter
    }
    

    and, of course, make sure you pass arguments to it as tuples.

提交回复
热议问题