Skip to content

Instantly share code, notes, and snippets.

@gseitz
Forked from kjetilv/gist:434620
Created June 19, 2010 14:31
Show Gist options
  • Save gseitz/444935 to your computer and use it in GitHub Desktop.
Save gseitz/444935 to your computer and use it in GitHub Desktop.
// A simple trait:
trait CascadingActions {
implicit def tToActioneerT[T](t: T) = Actioneer(t)
case class Actioneer[T](tee: T) {
def withAction(action: (T => Unit)): T =
withActions(action)
def withActions(actions: (T => Unit)*): T = {
actions foreach (_ (tee))
tee
}
}
}
// Yet with nice properties when you're dealing with
// an object that take a lot of setters, and you really
// wish it supported cascading:
def newStupid = {
val stupidTemporaryVariable = new StupidObject
stupidTemporaryVariable setSillyProperty "foo"
stupidTemporaryVariable setAnotherOne "bar"
stupidTemporaryVariable // mention it AGAIN here, just so it gets returned
}
// But instead:
def newStupid = new StupidObject withActions(
_ setSillyProperty "foo",
_ setAnotherOne "bar")
// Wow!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment