“twitter_util_eval_is_cool”

fujohnwang

2012-03-28


You define a scala trait:

trait ServerConfig extends (() => Server) {
  var port: Int = 9999
  var timeout: Option[Duration] = None

  def apply(): Server = new Server(port, timeout, ...)
}

You define a new implementation for the trait in a scala source code file which will be used as configuration file:

new ServerConfig {
  port = 12345
  timeout = 250.milliseconds
}

Then you can use Eval to bind your configuration and bootstrap together:

  val eval = new Eval()
  val config: ServerConfig = eval[ServerConfig](new File("..."))
  val server: Server = config()
  server.start()

Now it’s done!

Neat and cool, typesafe-proof configuration with scala as configuration DSL! Don’t you like it?! ^_^

References

  1. Why Config?
  2. https://github.com/twitter/util

>>>>>> 更多阅读 <<<<<<


「福强私学」来一个?

「福强私学」, 一部沉淀了个人成长、技术与架构、组织与管理以及商业上的方法与心法的百科全书。


开天窗,拉认知,订阅「福报」,即刻拥有自己的全模态人工智能。

订阅「福报」