Created
April 25, 2017 13:48
-
-
Save TomLous/d0f52c6df76cad50aa74bb56e21dac2a to your computer and use it in GitHub Desktop.
Example of curried class constructors
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 Parser{ | |
| def parse(i: Int):String | |
| } | |
| object ParserA extends Parser{ | |
| override def parse(i: Int): String = s"A: $i" | |
| } | |
| object ParserB extends Parser{ | |
| override def parse(i: Int): String = s"B: $i" | |
| } | |
| case class CurriedClass(parser: Parser)(y: Int) { | |
| def show = parser.parse(y) | |
| } | |
| val extendedClassA = CurriedClass(ParserA) _ | |
| val extendedClassB = CurriedClass(ParserB) _ | |
| val n = extendedClassA(3) | |
| val m = extendedClassA(5) | |
| val o = extendedClassB(2) | |
| n.show | |
| m.show | |
| o.show | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment