Skip to content

Instantly share code, notes, and snippets.

@oscarrenalias
Created October 2, 2012 21:40
Show Gist options
  • Select an option

  • Save oscarrenalias/3823482 to your computer and use it in GitHub Desktop.

Select an option

Save oscarrenalias/3823482 to your computer and use it in GitHub Desktop.
Type-safe configuration in Scala
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