[英]Scala implicits
I'm trying to understand how to use implicits in Scala: In my code I have:我试图了解如何在 Scala 中使用隐式:在我的代码中,我有:
trait Cell
class EnemyCell extends Cell
class RectangleCell extends Serializable with Cell
I defined a trait Placeable and I wanted it to have a subtype of cell我定义了一个 trait Placeable 并且我希望它有一个 cell 的子类型
trait Placeable[A <: Cell] {
def place(a: A, cell:Option[RectangleCell],controller:MapController ): Unit
}
I then defined the object:然后我定义了对象:
object Placeable {
//Apply
def apply[A <: Cell](implicit pleaceable: Placeable[A ]): Placeable[A] = pleaceable
def place[A: Placeable](a: A, cell:Option[RectangleCell], controller:MapController) = Placeable[A].place(a, cell, controller)
def instance[A <: Cell](func: (A, Option[RectangleCell], MapController) => Unit): Placeable[A] =
new Placeable[A] {
def place(a: A, cell:Option[RectangleCell] , controller: MapController): Unit = func(a, cell, controller)
}
//These 2 def does not seem to do anything...
implicit def cellToEnemyCell(cell: Cell): EnemyCell = cell.asInstanceOf[EnemyCell];
implicit def cellToRectangleCell(cell: Cell): RectangleCell = cell.asInstanceOf[RectangleCell];
implicit val rectanglePlaceable: Placeable[RectangleCell] =
instance((selected, cell, controller) => {
...
})
implicit val enemyPlaceable: Placeable[EnemyCell] =
instance((selected, cell, controller) => {
...
})
}
The problem is that I am getting the error:问题是我收到错误:
Error:(75, 98) type arguments [A] do not conform to method apply's type parameter bounds [A <: model.Cell]
def place[A: Placeable](a: A, cell:Option[RectangleCell], controller:MapController) = Placeable[A].place(a, cell, controller)
All I wanted was to use:我只想使用:
var _selected:Option[Cell] = Option.empty; //I edit this in my code and it can be either a RectangleCell or a EnemyCell
...
val tmpRect = _selected.get
place(tmpRect,cell,this)
instead of doing:而不是做:
if(_selected.get.isInstanceOf[RectangleCell]) {
val tmpRect = _selected.get.asInstanceOf[RectangleCell]
place(tmpRect,cell,this)
} else {
val tmpRect = _selected.get.asInstanceOf[EnemyCell]
place(tmpRect,cell,this)
}
Thank you in advance!先感谢您!
Here's the fix that gets it to compile.这是让它编译的修复程序。
def place[A <: Cell :Placeable](...
I'm not sure if it gets you where you want to be ( _selected
?) but at least it compiles.我不确定它是否能让你到达你想去的地方( _selected
?)但至少它可以编译。
Here's the reason for the error.这是错误的原因。 (I'll use different type parameter designations. Reusing A
over and over again can be confusing when they don't necessarily mean the same thing.) (我将使用不同的类型参数名称。一遍又一遍地重复使用A
可能会造成混淆,因为它们不一定表示同一件事。)
trait Placeable[C <: Cell] {...
- The Placeable
type parameter is restricted to Cell
or its sub-types. trait Placeable[C <: Cell] {...
- Placeable
类型参数仅限于Cell
或其子类型。def place[P: Placeable](...
- There must be a Placeable[P]
available in the implicit scope. But P
is unrestricted. That conflicts with the Placeable
definition. def place[P: Placeable](...
- 在隐式范围内必须有一个可用的Placeable[P]
。但P
不受限制。这与Placeable
定义冲突。update : No amount of implicit magic is going to resolve your problem because it's a compiler typing issue, unrelated to implicits.更新:再多的隐式魔术也无法解决您的问题,因为这是一个编译器类型问题,与隐式无关。
If _selected
is type Option[Cell]
then _selected.get
is type Cell
and, as far as the compiler is concerned, that's the end of the story.如果_selected
是Option[Cell]
类型,那么_selected.get
是Cell
类型,就编译器而言,这就是故事的结尾。 If there's actually an underlying sub-type here, that can be teased out at run-time.如果这里实际上有一个底层子类型,则可以在运行时对其进行梳理。
_selected match {
case Some(rc:RectangleCell) => place(rc,cell,this)
case Some(ec:EnemyCell) => place(ec,cell,this)
case _ => //some default action
}
Implicits are resolved at compile-time so they can't help discover the underlying type.隐式是在编译时解析的,因此它们无助于发现底层类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.