Created
January 11, 2016 17:18
-
-
Save fancellu/c5fd4fba45297dc658d1 to your computer and use it in GitHub Desktop.
Example of adding optional new behavior to old method without breaking API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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