加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 匿名函数的参数类型

发布时间:2020-12-16 09:02:30 所属栏目:安全 来源:网络整理
导读:我在使用这段代码时遇到了一些麻烦. 它应该是一个带有Elements BinaryOperations和UnaryOperations的OperationTree. 方法eval进行评估并在地图中查找变量. 这是代码 1 import collection.immutable.HashMap 2 sealed abstract class OpTree[T]{ 3 4 def eval
我在使用这段代码时遇到了一些麻烦.
它应该是一个带有Elements BinaryOperations和UnaryOperations的OperationTree.
方法eval进行评估并在地图中查找变量.

这是代码

1 import collection.immutable.HashMap
  2 sealed abstract class OpTree[T]{
  3 
  4   def eval(v:HashMap[Char,T]):T = {
  5     case Elem(x) => x
  6     case UnOp(f,c) => {
  7       f(c.eval(v))
  8     }
  9     case BinOp(f,l,r) => {
 10       f(l.eval(v),r.eval(v))
 11     }
 12     case Var(c) => {
 13       v.get(c)
 14     }
 15   }
 16 }
 17 //Leaf
 18 case class Elem[T](elm:T) extends OpTree[T]
 19 //Node with two sons
 20 case class UnOp[T](f:T => T,child:OpTree[T]) extends OpTree[T]
 21 //Node with one son
 22 case class BinOp[T](f:(T,T) => T,left:OpTree[T],right:OpTree[T]) extends OpTree[T]
 23 case class Var[T](val c:Char) extends OpTree[T]

编译说:

OpTree.scala:4: error: missing parameter type for expanded function
The argument types of an anonymous function must be fully known. (SLS 8.5)
Expected type was: T
  def eval(v:HashMap[Char,T]):T = {
                                  ^
one error found

有什么建议??

谢谢!

解决方法

你忘记了真正匹配的东西……

你的代码:

def eval(v:HashMap[Char,T]):T = {

必要的代码:

def eval(v:HashMap[Char,T]):T = v match {

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读