我正在阅读“Scala编程”,其中一个代码示例如下:
以下代码会出现此错误:
Multiple markers at this line
- type mismatch; found : Unit required: B
- type mismatch; found : Unit required: B
abstract class CustomMap[A, B] extends Map[A, B] {
def get(key: A) : Option[B] = {
if (contains(key)){
new Some(getValue(key))
}
else
None
}
def getValue(key : A) = {
}
}
错误发生在以下行:
new Some(getValue(key))
返回类型应采用什么形式?我试过返回int&字符串编译器不接受它。
答案 0 :(得分:1)
正确的语法是Some(getValue(key))
。
答案 1 :(得分:1)
除了Tass指出的错误之外,这看起来像是你没有实现getValue
的事实。尝试将getValue
定义为
def getValue(key: A): B = ???
直到您准备好提供实施。
我对你的问题感到困惑,“返回类型应采用什么形式?”。你的意思是getValue
的返回类型?它应该返回B
。