匿名函数的参数类型 [英] Argument type of anonymous function

查看:27
本文介绍了匿名函数的参数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用此代码时遇到了一些问题.它应该是一个带有元素 BinaryOperations 和 UnaryOperations 的 OperationTree.方法 eval 进行评估并在地图中查找变量.

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.

这是代码

 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

有什么建议吗??

谢谢!

推荐答案

您忘记实际匹配某些内容...

You have forgotten to actually match something...

您的代码:

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

必要的代码:

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

这篇关于匿名函数的参数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆