Skip to content

Instantly share code, notes, and snippets.

@dohzya
Created November 25, 2014 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dohzya/d9de26a5b4a888d271ab to your computer and use it in GitHub Desktop.
Save dohzya/d9de26a5b4a888d271ab to your computer and use it in GitHub Desktop.
// B must be defined when calling foo
def foo[A, B](a: A) = (f: A => B) => f(a)
// Do not do “val foo3 = foo(3)”: the type of B will be Nothing
val foo3 = foo[Int, Int](3)
foo3 { x => x * 2 }
// impossible to do something like “foo3 { x => s"(%x)" }”
// defining Forall
trait Forall[F[_]] { def apply[A]: F[A] }
// The definition of bar it a bit more complex :-(
def bar[A](a: A) = new Forall[({ type F[B] = (A => B) => B })#F] {
def apply[B]: (A => B) => B = f => f(a)
}
// calling bar (the type of “3” is the only one that have to be defined)
var bar3 = bar(3)
// calling bar3 with a function which returns an Int
bar3[Int] { x => x * 2 }
// calling bar3 with a function which returns a String
bar3[String] { x => s"(%x)" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment