0

With a type like trait A[T], finding an implicit in scope is simply implicitly[A[SomeType]]

Can this be done and, if so, how is this done where the type-parameter is replaced with an abstract type member, like in trait A { type T }?

megri
  • 1,019
  • 8
  • 10

2 Answers2

2

You can do

implicitly[A { type T = Int }

but you risk to lose precision:

scala> trait Foo { type T ; val t: T }
defined trait Foo

scala> implicit val intFoo: Foo { type T = Int } = new Foo { type T = Int ; val t = 23 }
intFoo: Foo{type T = Int} = $anon$1@6067b682

scala> implicitly[Foo].t // implicitly loses precision
res0: Foo#T = 23

To solve this problem, you can use the newly introduced the method, from the shapeless library (from which I've take the example above)

scala> the[Foo].t // the retains it
res1: Int = 23

scala> the[Foo].t+13
res2: Int = 36
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
1

I just realized this can be done with implicitly[A { type T = SomeType }]

megri
  • 1,019
  • 8
  • 10