Scala 错误:找到列表 [Char],需要列表 [ScalaObject]
我有这段 Scala 代码片段:
def prologList(l: List[ScalaObject], sep: String) =
"[" + (if (l isEmpty) "" else l.reduceLeft(_ + sep + _)) + "]"
def neighbors(s: State) = prologList(trans(s).toList, ", ")
def labels(s: State) = prologList(labeling(s).toList, ", ")
倒数第二行编译正常,但在最后一行出现错误
找到
List[Char]
,需要List[ScalaObject]
(labeling
的类型为 Map[State, Set[Char]]< /code>。)
我有点惊讶,因为 1)我认为 List[Char]
可以被视为 List[ScalaObject]
的子类型(而不是到 Java),2)最后一行上面的行编译! (虽然 trans
的类型为 Map[State, Set[State]]
...)
问题很明显,我做错了什么,以及如何修复它?
I have this snippet of Scala code:
def prologList(l: List[ScalaObject], sep: String) =
"[" + (if (l isEmpty) "" else l.reduceLeft(_ + sep + _)) + "]"
def neighbors(s: State) = prologList(trans(s).toList, ", ")
def labels(s: State) = prologList(labeling(s).toList, ", ")
The next-to-last line compiles fine, but on the last line I get the error
Found
List[Char]
, requiredList[ScalaObject]
(labeling
has the type Map[State, Set[Char]]
.)
I'm a bit surprised, since 1) I thought that List[Char]
could be seen as a subtype of List[ScalaObject]
(as opposed to Java), and 2) the line above the last line compiles! (trans
has type Map[State, Set[State]]
though...)
The question is obvious, what am I doing wrong, and how do I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Char
不是ScalaObject
的子类型。顶部有
Any
,它是所有内容的超类型。您可能可以将ScalaObject
替换为Any
,这样您的代码就可以编译。请参阅 http://www.scala-lang.org/node/128 了解类型层次结构图。
在 REPL 中,您可以使用隐式函数来解决类型关系问题:
编辑:顺便说一句,您知道
mkString
吗?Char
is not a subtype ofScalaObject
.At the top you have
Any
which a super type of everything. You can probably replaceScalaObject
withAny
and that should make your code compile.See http://www.scala-lang.org/node/128 for a type hierarchy diagram.
In the REPL you can use the implicitly function to troubleshoot type relationships:
Edit: by the way, do you know about
mkString
?