scala - Implicits, pimpl pattern etc -
let's have these classes:
case class a() case class b() case class c(a: a, b: b)
and these variables:
val = a() val b = b()
is there way instance of c
implicitly , without making a
, b
implicit vals? i.e. if have method expecting c
:
def foo(c: c)
the case class a
notation deprecated. have use case class a()
, otherwise assigning a
val a
result in a
referring companion object of case class a
generated behind scene.
it's understanding wanted a
refer instance of case class, not companion object.
if so, you're asking possible - a
, b
don't have implicit, have add new implicit method scope:
implicit def obtainc = new c(a, b)
then, have put implicit
modifier c
in method foo
:
def foo(implicit c: c)
complete session:
scala> case class a() defined class scala> case class b() defined class b scala> case class c(a: a, b: b) defined class c scala> val = a() a: = a() scala> val b = b() b: b = b() scala> implicit def obtainc = new c(a, b) obtainc: c scala> def foo(implicit c: c) = {} foo: (implicit c: c)unit scala> foo
Comments
Post a Comment