のような型trait A[T]
では、スコープ内の暗黙的なものを見つけるのは簡単ですimplicitly[A[SomeType]]
これを行うことができますか?もしそうなら、型パラメータが抽象型メンバーに置き換えられた場合、どのように行われtrait A { type T }
ますか?
のような型trait A[T]
では、スコープ内の暗黙的なものを見つけるのは簡単ですimplicitly[A[SomeType]]
これを行うことができますか?もしそうなら、型パラメータが抽象型メンバーに置き換えられた場合、どのように行われtrait A { type T }
ますか?
できるよ
implicitly[A { type T = Int }
ただし、精度を失うリスクがあります。
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
この問題を解決するには、ライブラリから新しく導入されたthe
メソッドを使用できます (そこから上記の例を取り上げました)。shapeless
scala> the[Foo].t // the retains it
res1: Int = 23
scala> the[Foo].t+13
res2: Int = 36
これができることに気づきましたimplicitly[A { type T = SomeType }]