[英]None type in Priority queue scala
I'm using a priority queue to order a case class called TreeNodeWithCostAndHeuristic 我正在使用优先级队列来命令一个名为TreeNodeWithCostAndHeuristic的案例类
case class TreeNodeWithCostAndHeuristic[S,A](parent:Option[TreeNodeWithCostAndHeuristic[S,A]],
action: Option[A],
state: S,
cost: Double,
estimatedRemainingCost: Double)
This priority queue is created inside a function that uses its parameter to set the initial state while the other values have to be kept as None or 0 此优先级队列在函数内创建,该函数使用其参数设置初始状态,而其他值必须保持为None或0
def HeuristicGraphSearch[S,A](problem: ProblemWithCostAndHeuristic[S,A]) = {
val root = TreeNodeWithCostAndHeuristic(parent = None,action=None,state=problem.initialState,cost = 0.0, estimatedRemainingCost = 0.0)
val frontier : mutable.PriorityQueue[TreeNodeWithCostAndHeuristic[S,A]] = mutable.PriorityQueue.empty[TreeNodeWithCostAndHeuristic[S,A]]
frontier.enqueue(root)
However because parent and action are none I get a mismatch between expected type TreeNodeWithCostAndHeuristic[S,A]
and the one I'm trying to enqueue TreeNodeWithCostAndHeuristic[S,Nothing]
. 但是因为父和动作都没有,我得到了预期类型
TreeNodeWithCostAndHeuristic[S,A]
与我试图将TreeNodeWithCostAndHeuristic[S,Nothing]
排队的不匹配。
As far as I know Nothing is a subtype of Option and in my case class both parent and action are options. 据我所知,Nothing是Option的子类型,在我的case类中,parent和action都是选项。 Why am I getting the mismatch?
为什么我会遇到不匹配?
It's related to the way Scala compiler infers types. 它与Scala编译器推断类型的方式有关。 Short answer is to simply help it out a bit by explicitly declaring the types when constructing your case class:
简短的回答是通过在构造case类时明确声明类型来简单地帮助它:
val root = TreeNodeWithCostAndHeuristic[S, A](parent = None,action=None,state=problem.initialState,cost = 0.0, estimatedRemainingCost = 0.0)
Reason why TreeNodeWithCostAndHeuristic[S, Nothing]
is not considered a valid substitute for TreeNodeWithCostAndHeuristic[S, A]
is because it's not its subclass; TreeNodeWithCostAndHeuristic[S, Nothing]
不被认为是TreeNodeWithCostAndHeuristic[S, A]
的有效替代的原因是因为它不是它的子类; to be one, it would have to be covariant in type A. If some Foo[A]
is covariant in its type A
, only then the following holds: Foo[S] <: Foo[A]
for any S
that is subclass of A
. 如果是一个,它必须在类型A中是协变的。如果一些
Foo[A]
在其类型A
是协变A
,那么只有以下成立: Foo[S] <: Foo[A]
对于任何S
是子类的A
In addition to sloucs answer, you can also use type ascription to help the compiler out: 除了sloucs回答,您还可以使用类型归属来帮助编译器:
val root = TreeNodeWithCostAndHeuristic(
parent = None: Option[TreeNodeWithCostAndHeuristic[S, A]],
action = None: Option[A],
state = problem.initialState,
cost = 0.0,
estimatedRemainingCost = 0.0)
The reason the compiler is complaining is that it has no way to guarantee None.type
is a Option[TreeNodeWithCostAndHeuristic[S, A]]
and Option[A]
, respectively, and thus it "bails out" and infers Nothing
, which is the bottom type of all types. 编译器抱怨的原因是它无法保证
None.type
是 Option[TreeNodeWithCostAndHeuristic[S, A]]
和Option[A]
,因此它“拯救”并推断出Nothing
,这就是所有类型的底部类型。
@Dima also suggests using Option.empty[A]
: @Dima还建议使用
Option.empty[A]
:
val root = TreeNodeWithCostAndHeuristic(
parent = Option.empty,
action = Option.empty[A],
state = problem.initialState,
cost = 0.0,
estimatedRemainingCost = 0.0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.