Created
July 11, 2012 13:51
-
-
Save bkyrlach/3090465 to your computer and use it in GitHub Desktop.
I had no idea...
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
| trait Invert extends AddOperation { | |
| override abstract def add(a: Int, b: Int): Int = super.add(a * -1, b * -1) | |
| } | |
| trait MultDiv extends AddOperation { | |
| override abstract def add(a: Int, b: Int): Int = super.add(a * 2, b / 2) | |
| } | |
| trait Inc2Dec extends AddOperation { | |
| override abstract def add(a: Int, b: Int): Int = super.add(a + 2, b -1) | |
| } | |
| class AddOperation { | |
| def add(a: Int, b: Int): Int = a + b | |
| } | |
| object Crazy extends App { | |
| val a = new AddOperation | |
| println(a.add(10, 11)) //21 | |
| val b = new AddOperation with Inc2Dec | |
| println(b.add(10, 11)) //22 | |
| val c = new AddOperation with Inc2Dec with MultDiv | |
| println(c.add(10, 11)) //26 | |
| val d = new AddOperation with MultDiv with Inc2Dec | |
| println(d.add(10, 11)) //29 | |
| val e = new AddOperation with Invert with Inc2Dec | |
| println(e.add(10, 11)) //-22 | |
| val f = new AddOperation with Inc2Dec with Invert | |
| println(f.add(10, 11)) //-20 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment