Scala 中的case关键字在这里是什么意思
1个回答
展开全部
(1 to 10) map { case x => 2*x }
中的{ case x => 2*x } 得到的是一个PartialFunction 。然而,事实并非如此。
简单说,凡是Scala中出现如下表达式时:
{ case p1 => e1; case p2 => e2; ...; case pn => en }
其结果为Scala中的匿名函数(Anonymous Function) ,但是其具体类型是根据其所处位置而定,可能是一个FunctionN,也可能是一个PartialFunction (这里的FunctionN指的是非PartialFunction的Function)。
下面用一段小代码证明之:
scala> def needfunc[F](f: F) = fneedfunc: [F](f: F)Fscala> needfunc[PartialFunction[Int, Int]]({ case x => 2*x }).getClass.getSuperclasswarning: there was one feature warning; re-run with -feature for detailsres0: Class[?0] forSome { type ?0 >: ?0; type ?0 <: PartialFunction[Int,Int] } = class scala.runtime.AbstractPartialFunction$mcII$spscala> needfunc[Int => Int]({ case x => 2*x }).getClass.getSuperclasswarning: there was one feature warning; re-run with -feature for detailsres1: Class[?0] forSome { type ?0 >: ?0; type ?0 <: Int => Int } = class scala.runtime.AbstractFunction1$mcII$sp
上面的结果表明,当我需要一个Function(非PartialFunction)时,我得到的就是一个非PartialFunction的Function;当我需要一个PartialFunction时,得到就是PartialFunction。
当然,我不是仅有上面的例子就下结论。实际上,《Scala语言规范》(Scala Language Specification)对此有具体规定,引用如下:Pattern Matching
Pattern Matching Anonymous Functions
BlockExpr ::= `{' CaseClauses `}'
An anonymous function can be defined by a sequence of cases
{ case p1 => b1; …… case pn => bn }
which appear as an expression without a prior match. The expected type of such an expression must in part be defined. It must be either scala.Functionkk[S1,…,Sk, R] for some k>0, or scala.PartialFunction[S1, R], where the argument type(s) S1,…,Sk must be fully determined, but the result type R may be undetermined.
为啥需要搞清什么时候是PartialFunction而什么时候是FunctionN呢,这里涉及另一个问题:PartialFunction在某些时候相对FunctionN会存在性能问题!
这是另一个有趣的问题了。
中的{ case x => 2*x } 得到的是一个PartialFunction 。然而,事实并非如此。
简单说,凡是Scala中出现如下表达式时:
{ case p1 => e1; case p2 => e2; ...; case pn => en }
其结果为Scala中的匿名函数(Anonymous Function) ,但是其具体类型是根据其所处位置而定,可能是一个FunctionN,也可能是一个PartialFunction (这里的FunctionN指的是非PartialFunction的Function)。
下面用一段小代码证明之:
scala> def needfunc[F](f: F) = fneedfunc: [F](f: F)Fscala> needfunc[PartialFunction[Int, Int]]({ case x => 2*x }).getClass.getSuperclasswarning: there was one feature warning; re-run with -feature for detailsres0: Class[?0] forSome { type ?0 >: ?0; type ?0 <: PartialFunction[Int,Int] } = class scala.runtime.AbstractPartialFunction$mcII$spscala> needfunc[Int => Int]({ case x => 2*x }).getClass.getSuperclasswarning: there was one feature warning; re-run with -feature for detailsres1: Class[?0] forSome { type ?0 >: ?0; type ?0 <: Int => Int } = class scala.runtime.AbstractFunction1$mcII$sp
上面的结果表明,当我需要一个Function(非PartialFunction)时,我得到的就是一个非PartialFunction的Function;当我需要一个PartialFunction时,得到就是PartialFunction。
当然,我不是仅有上面的例子就下结论。实际上,《Scala语言规范》(Scala Language Specification)对此有具体规定,引用如下:Pattern Matching
Pattern Matching Anonymous Functions
BlockExpr ::= `{' CaseClauses `}'
An anonymous function can be defined by a sequence of cases
{ case p1 => b1; …… case pn => bn }
which appear as an expression without a prior match. The expected type of such an expression must in part be defined. It must be either scala.Functionkk[S1,…,Sk, R] for some k>0, or scala.PartialFunction[S1, R], where the argument type(s) S1,…,Sk must be fully determined, but the result type R may be undetermined.
为啥需要搞清什么时候是PartialFunction而什么时候是FunctionN呢,这里涉及另一个问题:PartialFunction在某些时候相对FunctionN会存在性能问题!
这是另一个有趣的问题了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询