Created
April 8, 2017 20:08
-
-
Save jhegedus42/3aa01b78e271c7bd4851abf73c7f718d to your computer and use it in GitHub Desktop.
playing around with implicits
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import MyProducers.MapProducer | |
trait Entity | |
case class Ref[T](id: String) | |
case class IntEntity(int: Int) | |
case class StringEntity(s: String) | |
trait RefResolver { | |
private def getMap[T: MapProducer](): Map[Ref[T], T] = implicitly[MapProducer[T]].produceMap | |
def getEntity[T: MapProducer](r: Ref[T]): Option[T] = getMap[T]().get(r) | |
} | |
trait ImplicitTrait{ | |
implicit val ip: MapProducer[Int] | |
implicit val sp: MapProducer[String] | |
} | |
object Implicits extends ImplicitTrait{ | |
object StringMapProducer extends MapProducer[String] { | |
override def produceMap: Map[Ref[String], String] = | |
Map(Ref[String]("bla") -> "stuff", Ref[String]("pina") -> "Shiraly") | |
} | |
object IntMapProducer extends MapProducer[Int] { | |
override def produceMap = | |
Map(Ref[Int]("bla") -> 42, Ref[Int]("pina") -> 43) | |
} | |
implicit val ip=IntMapProducer | |
implicit val sp=StringMapProducer | |
} | |
trait ImplicitsWithImports{ | |
val implicits: ImplicitTrait | |
} | |
trait Main extends ImplicitsWithImports{ | |
import implicits._ | |
object RefResolverImpl extends RefResolver | |
val stuffInt = RefResolverImpl.getEntity[Int](Ref("bla")) | |
val stuffString = RefResolverImpl.getEntity[String](Ref("pina")) | |
println(stuffInt) | |
println(stuffString) | |
} | |
object Main2{ | |
object mainCoon extends Main{ | |
val implicits = Implicits | |
} | |
import mainCoon.implicits._ | |
mainCoon.RefResolverImpl.getEntity[Int](Ref("pina")) | |
} | |
object MyProducers { | |
trait ProducerImplicits { | |
implicit val sp: MapProducer[String] | |
implicit val ip: MapProducer[Int] | |
} | |
trait MapProducer[T] { | |
def produceMap: Map[Ref[T], T] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment