scala - PartialFunction type -
in scala play framework seen code:
abstract class analyserinfo case class columnc(typename:string,fieldname:string) extends analyserinfo case class tablec(typename:string) extends analyserinfo val asis :partialfunction[analyserinfo,string] = { case columnc(_,f) => f; case tablec(typename) => typename }
what difference with:
val asis: (analiserinfo)=>string = (info) => info match { case columnc(_,f) => f; case tablec(typename) => typename }
there preferred style? , why in first case match keyword can omitted?
thank support.
double => double
shorthand function[double, double]
. partialfunction
inherits function
adds few methods. importantly, adds method isdefinedat
allows query if function defined parameter.
the case
s without match special syntax define partial functions, generates isdefinedat
returns true
matching case
s.
say have function returns 1/x, positive values of x, define as:
scala> val f: (double => double) = { case x if x > 0 => 1/x } f: (double) => double = <function1>
or as:
scala> val g: partialfunction[double, double] = { case x if x > 0 => 1/x } g: partialfunction[double,double] = <function1>
the second version has benefit check if function applicable parameter:
scala> g.isdefinedat(-3) res0: boolean = false
this feature example used in scala implement actor library actor might consume types of messages.
Comments
Post a Comment