Created
March 14, 2016 14:25
-
-
Save paul-lysak/d41dbd8c388b4e5fd97d to your computer and use it in GitHub Desktop.
Example of separation between data and behavior with implicit conversions
This file contains 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
SomeData.scala: | |
case class SomeData(a: Int, b: Int) | |
AdditionExtensions.scala: | |
object AdditionExtensions { | |
implicit class SomeDataConversion(d: SomeData) { | |
def add = d.a + d.b | |
def add1 = d.a + d.b +1 | |
... | |
} | |
} | |
some other class: | |
import AdditionExtensions._ | |
val myData = SomeData(2, 3) | |
println(myData.add) //5 | |
println(myData.add1) //6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment