【发布时间】:2018-05-26 13:18:03
【问题描述】:
我正在尝试在 Scala 中实现基于基于权重的树的集合。我有以下类层次结构:
case class Node[K, V](key: K, value: Option[V] = None, left: Option[Node[K, V]] = None, right: Option[Node[K, V]] = None, size: Int = 1)(implicit ord: K => Ordered[K])
case class Tree[K, V](root: Option[Node[K, V]] = None, alpha: Double = 0.25)(implicit ord: K => Ordered[K])
case class WBTreeSet[K](tree: Tree[K, Nothing] = Tree[K, Nothing]())(implicit ord: K => Ordered[K])
类隐式获取Ordered[K],以便我可以比较类型 K 的元素并构建树。
当我尝试编译代码时,出现以下错误:
No implicit view available from K => Ordered[K].
Error occurred in an application involving default arguments.
case class WBTreeSet[K](tree: Tree[K, Nothing] = Tree[K, Nothing]())(implicit ord: K => Ordered[K]) {
not enough arguments for method apply: (implicit ord: K => Ordered[K])Tree[K,Nothing] in object Tree.
Unspecified value parameter ord.
Error occurred in an application involving default arguments.
case class WBTreeSet[K](tree: Tree[K, Nothing] = Tree[K, Nothing]())(implicit ord: K => Ordered[K]) {
当我删除WBTreeSet 中tree 的默认值时,代码会编译。
我是第一次使用 Scala,我已经搜索了很多类似的问题,但是我仍然无法解决这个问题。有没有办法在不改变类结构的情况下解决这个问题,或者我应该用另一种方式来解决这个问题?
【问题讨论】:
标签: scala class generics case implicit