Skip to content

Instantly share code, notes, and snippets.

@kubukoz
Created July 15, 2017 18:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kubukoz/c11732d3c887be506358f7de94f29551 to your computer and use it in GitHub Desktop.
Save kubukoz/c11732d3c887be506358f7de94f29551 to your computer and use it in GitHub Desktop.
object OneWayImplicits {
//outside implicits visible, but T is not visible inside
case class Outwards[T](value: T) extends AnyVal
//outside implicits invisible, T is visible inside
case class Inwards[T](value: T) extends AnyVal
implicit def outwardsFromT[T](implicit t: T): Outwards[T] = Outwards(t)
implicit def tFromInwards[T](implicit inw: Inwards[T]): T = inw.value
sealed trait X
def usesOutwards()(implicit ow: Outwards[X]) = {
//X is not implicitly visible here
val x: X = ow.value
}
//doesn't get X from implicit scope
def usesInwards(implicit iw: Inwards[X]) = {
//but X is implicitly visible here
???
}
def composability(implicit x: Inwards[X]) = {
usesOutwards() //gets `Outwards(x.value)`
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment