Created
October 2, 2012 21:40
-
-
Save oscarrenalias/3823482 to your computer and use it in GitHub Desktop.
Type-safe configuration in Scala
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 Handler | |
| class Handler1 extends Handler | |
| class Handler2 extends Handler | |
| trait HandlerConfiguration { | |
| val handler1: Handler | |
| val handler2: Handler | |
| } | |
| type Configured[A] = HandlerConfiguration => A | |
| object MyConfiguration extends HandlerConfiguration { | |
| override val handler1 = new Handler1 | |
| override val handler2 = new Handler2 | |
| } | |
| class ClassThatNeedsAHandler(handler: Handler) { | |
| def f = handler.getClass.getName | |
| } | |
| val generator: Configured[ClassThatNeedsAHandler] = handlerConfig => new ClassThatNeedsAHandler(handlerConfig.handler1) | |
| val myClass = generator(MyConfiguration) | |
| myClass.f /* should say "Handler1" */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment