Skip to content

Instantly share code, notes, and snippets.

@fancellu
Created January 11, 2016 17:18
Show Gist options
  • Save fancellu/c5fd4fba45297dc658d1 to your computer and use it in GitHub Desktop.
Save fancellu/c5fd4fba45297dc658d1 to your computer and use it in GitHub Desktop.
Example of adding optional new behavior to old method without breaking API
// say you have a method
def hgetall(key:String,value:String)
// but you want to optionally alter its behaviour, whilst keeping old behaviour as default, hence breaking no existing code
sealed trait Behavior
object Behaviors{
implicit case object NewB extends Behavior
implicit case object OldB extends Behavior
}
def hgetall(key:String,value:String)(implicit behave:Behavior=Behaviors.OldB)={
println(behave) // put your code here
}
hgetall("xxx","yyy") //> OldB
import Behaviors.NewB
hgetall("xxx","yyy") //> NewB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment