Skip to content

Instantly share code, notes, and snippets.

@fwbrasil
Last active December 10, 2015 02:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fwbrasil/4368885 to your computer and use it in GitHub Desktop.
Save fwbrasil/4368885 to your computer and use it in GitHub Desktop.
package net.fwbrasil.sReflection
object CascadeImplicits {
implicit def toYourself[T](value: T): Yourself[T] = new Yourself(value)
implicit def toCascade[T](value: T): Cascade[T] = new Cascade(value)
class Yourself[T](value: T) {
def yourself = value
}
class Cascade[T](value: T) {
def cascade[R1](
f1: T => R1) =
f1(value)
def cascade[R1, R2](
f1: T => R1,
f2: T => R2) = {
f1(value)
f2(value)
}
def cascade[R1, R2, R3](
f1: T => R1,
f2: T => R2,
f3: T => R3) = {
f1(value)
f2(value)
f3(value)
}
def cascade[R1, R2, R3, R4](
f1: T => R1,
f2: T => R2,
f3: T => R3,
f4: T => R4) = {
f1(value)
f2(value)
f3(value)
f4(value)
}
def cascade[R1, R2, R3, R4, R5](
f1: T => R1,
f2: T => R2,
f3: T => R3,
f4: T => R4,
f5: T => R5) = {
f1(value)
f2(value)
f3(value)
f4(value)
f5(value)
}
}
}
import CascadeImplicits._
class CascadeImplicitsExample {
class Turtle {
def clear = {}
def forward(v: Int) = {}
def turn(v: Int) = {}
}
val turtle = new Turtle
val res: Turtle =
turtle.cascade(
_.clear,
_.forward(10),
_.turn(90),
_.forward(20),
_.yourself)
println(res)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment