scala - Importing generic implicits from class instances -




i'm trying make generic implicit provider can create implicit value given type, in lines of:

trait evidence[t]  class implicitprovider[t] {   class implementation extends evidence[t]   implicit val evidence: evidence[t] = new implementation } 

to use implicit, create val provider = new implicitprovider[t] instance necessary , import import provider._. works fine long there 1 instance. implicits several types needed in 1 place

case class a() case class b()  class test extends app {   val aprovider = new implicitprovider[a]   val bprovider = new implicitprovider[b]    import aprovider._   import bprovider._    val = implicitly[evidence[a]]   val b = implicitly[evidence[b]] } 

and fails compile could not find implicit value parameter , not enough arguments method implicitly errors.

if use implicit vals providers directly, starts work again.

implicit val aevidence = aprovider.evidence implicit val bevidence = bprovider.evidence 

however i'm trying avoid importing individual values, there several implicits inside each provider , goal abstract them if possible.

can achieved somehow or want compiler?

the issue when import both objects, you're bringing in 2 entities have colliding names: evidence in aprovider , evidence in bprovider. compiler cannot disambiguate those, both because of how implemented, , because it'd bad idea implicits, can arcane, able things cannot done explicitly (disambiguating between clashing names).

what don't understand point of implicitprovider is. can pull implementation class out top level , have object somewhere holds implicit vals.

class implementation[t] extends evidence[t]  object evidence {   implicit val aevidence: evidence[a] = new implementation[a]   implicit val bevidence: evidence[b] = new implementation[b] }  // usage: import evidence._ implicitly[evidence[a]] implicitly[evidence[b]] 

now, there no name clash.

if need have actual implicitprovider, can instead this:

class implicitprovider[t] { ... }  object implicitproviders {   implicit val aprovider = new implicitprovider[a]   implicit val bprovider = new implicitprovider[b]    implicit def implicitprovider2evidence[t: implicitprovider]: evidence[t]     = implicitly[implicitprovider[t]].evidence }  // usage import implicitproviders._ // ... 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -