Skip to content

Instantly share code, notes, and snippets.

@LeifW
Last active August 23, 2018 19:16
Show Gist options
  • Save LeifW/aefafa577b294a3fdd2b03cf39153cbc to your computer and use it in GitHub Desktop.
Save LeifW/aefafa577b294a3fdd2b03cf39153cbc to your computer and use it in GitHub Desktop.
Scala do-notation overloading
case class Foo(a: Int) {
def map(f : Int => Int) = Foo(f(a))
def flatMap(f : Int => Bar): Bar = f(a)
}
case class Bar(a: Int) {
def map(f : Int => Int) = Bar(f(a))
def flatMap(f : Int => Bar): Bar = f(a)
}
object Main {
val foo: Bar = for {
x <- Foo(1)
y <- Bar(x)
} yield y
def main(args: Array[String]): Unit = println(foo)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment