Using a context bound in a class type parameter

邮差的信 提交于 2020-01-11 09:14:08

问题


I was under the impression that context bounds would work only on methods:

trait Target[T]

class Post {
  def pinTo[T : Target](t:T)
}

apparently context bounds can be used inclass (and presumably trait) too:

trait Target[T]

class Post[T:Target] {
  def pintTo[T](t:T) 
}

Now I'm confused as to how the evidence can be provided to Post?

class Business
implicit object ev extends Target[Business] // is implicit necessary here ?

val p = new Post[Business] // ?? how do I provide ev ? 

related to Modeling a binary relationship between two types


回答1:


The A: Foo notation for context bounds is only a shortcut for asking for an implicit value parameter of type Foo[A]. Since traits do not have constructor value parameters, you can not use this with a trait:

trait Foo[A]

trait Bar[A: Foo] // "error: traits cannot have type parameters with context bounds..."

Whereas in classes it's possible:

class Bar[A: Foo] {
  def foo: Foo[A] = implicitly[Foo[A]]
}

Which is just a different way of writing

class Bar[A](implicit foo: Foo[A])

You provide the evidence like you do in any other normal method call:

new Bar[Int]()(new Foo[Int] {})  // explicitly

Or:

implicit val iFoo = new Foo[Int] {}

new Bar[Int]  // implicitly


来源:https://stackoverflow.com/questions/20440279/using-a-context-bound-in-a-class-type-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!