1
考慮:類型成員瓦特/隱
// type-class
trait Eq[A]
class MyInt
object MyInt {
implicit val myIntEq = new Eq[MyInt] {}
}
sealed trait Something {
type A
implicit val x: Eq[A]
}
case object SomethingImpl extends Something {
override type A = MyInt
override implicit val x = MyInt.myIntEq
}
然後,我用的類型成員通過的implicit
:
scala> def f(s: Something): Eq[s.A] = {
| implicit val x: Eq[s.A] = s.x
| x
| }
f: (s: Something)Eq[s.A]
然而,我的直覺告訴我,這是有些笨拙有帶來的隱性通過implicit val ...
進入範圍。
也許我應該在Something
的伴侶對象中定義f
函數?
什麼是定義此f
函數的標準方法?
你的'f'函數的例子似乎過於簡化了,因爲對於當前的信息,我會簡單地將它實現爲'= s.x'。 – sjrd
你可以'輸入s.x'。 –
@ Jasper-M這是答案,而不是評論。 –