Scala:隐式转换适用于 Any 吗?
我想将不同类型层次结构中的一些对象存储到 List[Any] 或类似容器中,但稍后对它们执行隐式转换以执行类似类型类的操作。 这是一个例子:
abstract class Price[A] {
def price(a: A): Int
}
trait Car
case class Prius(year: Int) extends Car
trait Food
case class FriedChicken() extends Food
object Def {
// implicit object AnyPrices extends Price[Any] {
// def price(any: Any) = 0
// }
// implicit object PriusPrices extends Price[Prius] {
// def price(car: Prius) = 100
// }
implicit object CarPrices extends Price[Car] {
def price(car: Car) = 100
}
implicit object FoodPrices extends Price[Food] {
def price(food: Food) = 5
}
}
def implicitPrice[A: Price](x: A) = implicitly[Price[A]].price(x)
import Def._
val stuff: List[Any] = List(Prius(2010), FriedChicken())
stuff map { implicitPrice(_) }
上面的代码抛出一个错误,如下所示:
error: could not find implicit value for evidence parameter of type Price[Any]
stuff map { implicitPrice(_) }
^
如果你取消注释 AnyPrices
,你会得到 List(0,0)
,但这不是我的意思期待。 我是否必须将清单存储到列表中才能正常工作?
此外, List(Prius(2010)) map {implicitPrice(_) }
也不起作用,因为它需要 Price[Prius]
和 Price[Car]
还不够好。有没有办法让它变得更灵活?
I would like to store some objects from different type hierarchy into List[Any]
or similar container, but perform implicit conversions on them later on to do something like type class.
Here is an example:
abstract class Price[A] {
def price(a: A): Int
}
trait Car
case class Prius(year: Int) extends Car
trait Food
case class FriedChicken() extends Food
object Def {
// implicit object AnyPrices extends Price[Any] {
// def price(any: Any) = 0
// }
// implicit object PriusPrices extends Price[Prius] {
// def price(car: Prius) = 100
// }
implicit object CarPrices extends Price[Car] {
def price(car: Car) = 100
}
implicit object FoodPrices extends Price[Food] {
def price(food: Food) = 5
}
}
def implicitPrice[A: Price](x: A) = implicitly[Price[A]].price(x)
import Def._
val stuff: List[Any] = List(Prius(2010), FriedChicken())
stuff map { implicitPrice(_) }
The above code throws an error as follows:
error: could not find implicit value for evidence parameter of type Price[Any]
stuff map { implicitPrice(_) }
^
If you uncomment AnyPrices
, you'd get List(0,0)
, but that's not what I am expecting.
Do I have to store the manifest into the list for this to work?
Also, List(Prius(2010)) map { implicitPrice(_) }
doesn't work either because it wants Price[Prius]
and Price[Car]
isn't good enough. Is there a way to make it more flexible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,一旦对象减少到
Any
,我就无法获得类型类。我使用Manifest
的尝试也失败了,因为即使我有T
,我似乎也无法将Any
转换为T
代码>Manifest[T] 对象。给了我
,所以看起来我必须先将它们解析为
Price
,然后再将它们放入List
中:So, looks like I can't get a type class once the objects are reduced to
Any
. My attempt of usingManifest
also failed, since there seems to be no way for me to cast anAny
intoT
even if I have theManifest[T]
object.gives me
so it seems like I have to resolve things into
Price
before I stick them intoList
: