匿名函数的参数类型
我在使用这段代码时遇到了一些问题。 它应该是一个带有元素 BinaryOperations 和 UnaryOperations 的操作树。 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
:有什么建议吗?
谢谢!
I'm having some trouble with this code.
It's supposed to be an OperationTree with Elements BinaryOperations and UnaryOperations.
The method eval does the evaluation and looks up the variables in a map.
Here's the code
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]
The Compiler says:
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
Any suggestions??
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您忘记了实际匹配某些内容...
您的代码:
必要的代码:
You have forgotten to actually match something...
Your code:
Necessary code: