Skip to content

Instantly share code, notes, and snippets.

@dflemstr
Forked from sadache/exampleExtensionMethod.scala
Created October 2, 2010 12:26
Show Gist options
  • Save dflemstr/607602 to your computer and use it in GitHub Desktop.
Save dflemstr/607602 to your computer and use it in GitHub Desktop.
//This doesn't actually work, since function application has the highest precedence in Scala.
def repeat(self: String, times: Int) = self * times
val x = "ABC"
x~repeat(5)
object Extension {
implicit def from[A](a: A): Extension[A] = Extension(a)
}
case class Extension[A](a: A) {
def ~[R](f: A => R): R = f(a)
def ~[R, B](f: (A, B) => R): B => R = f(a, _)
def ~[R, B, C](f: (A, B, C) => R): (B, C) => R = f(a, _, _)
def ~[R, B, C, D](f: (A, B, C, D) => R): (B, C, D) => R = f(a, _, _, _)
//...etc
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment