in scala play framework I seen this 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 is the difference with:
val asIs: (AnaliserInfo)=>String = (info) => info match {
case ColumnC(_,f) => f;
case TableC(typeName) => typeName
}
There is a preferred style? and why in the first case the match keyword can be omitted?
Thank you for the support.
Double => Doubleis just a shorthand forFunction[Double, Double].PartialFunctioninherits fromFunctionbut adds a few methods. Most importantly, it adds the methodisDefinedAtwhich allows you to query if the function is defined for some parameter.The
cases without a match are a special syntax to define partial functions, which generates anisDefinedAtthat returnstruefor all matchingcases.Say we have a function that returns 1/x, but only for positive values of x, we could it define as:
or as:
The second version has the benefit that we could check if the function is applicable to some parameter:
This feature is for example used in Scala to implement the actor library where an Actor might only consume certain types of messages.