Skip to content

Instantly share code, notes, and snippets.

@ElectricCoffee
Last active May 27, 2016 22:58
Show Gist options
  • Save ElectricCoffee/a9fadc660e7818f97318 to your computer and use it in GitHub Desktop.
Save ElectricCoffee/a9fadc660e7818f97318 to your computer and use it in GitHub Desktop.
Lets you call subroutines sequencially without code blocks, more info in the file
/*
* The premise of this is simple; it allows you to chain subroutines without using a block
* The idea comes from Haskell's Monads, where by language design you can't normally
* predict the order of statement execution due to the language being lazy,
* so instead an operator was added that allowed you to call statements sequencially
*
* Rather than writing something like this:
* def printStuff(): Unit = {
* println("blah")
* println("bloo")
* }
*
* You can write
* def printStuff(): Unit = println("blah") then println("bloo")
* or
* val x = y.init() then y.getName() // thanks to Lectori Salutem for this idea
*
* This saves an otherwise unnecessary code block, for an amount of code this small.
*/
object Rich {
implicit class RichAny(val left: Any) extends AnyVal {
def then[A](right: => A): A = right
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment