Scala 为什么我的代码不能识别上面声明的函数?
我在Scala中的方法调用方面遇到了一些问题 我尝试使用模式匹配方法来创建调车场问题的算法。我在类的开头声明了一个函数,用于确定令牌是否是运算符。然后,我尝试在函数中调用此函数以返回等式的反向波兰符号,但是,我得到了错误:Scala 为什么我的代码不能识别上面声明的函数?,scala,pattern-matching,Scala,Pattern Matching,我在Scala中的方法调用方面遇到了一些问题 我尝试使用模式匹配方法来创建调车场问题的算法。我在类的开头声明了一个函数,用于确定令牌是否是运算符。然后,我尝试在函数中调用此函数以返回等式的反向波兰符号,但是,我得到了错误: error: not found: value operatorTrue 这就是我的代码的外观: def operatorTrue(val : String) : Boolean = { * Code for function } def foo(* argu
error: not found: value operatorTrue
这就是我的代码的外观:
def operatorTrue(val : String) : Boolean = {
* Code for function
}
def foo(* arguments...) : Toks = {
* Code not relevant to the question
el match{
case operatorTrue(el) => ...
}
}
所以错误告诉我问题出在“operatorTrue(el)=>…”。为什么会这样?我很困惑,因为我已经声明了函数 在模式匹配中不能按原样使用函数,需要将其正确定义为提取器(cf
不应用
)
operatorTrue
不是模式匹配的有效案例,您希望使用。或者,如果运算符真(x)=>,您也可以情况x
。
object OpTrue {
def unapply(s: String): Boolean = ???
}
def foo(s: String) = s match {
case OpTrue() => ??? // ~> if (OpTrue.unapply(s)) ...
}