“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

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


欢迎加入「福强私学」

跨越2190个日夜,始终坚持“实践 + 原创”打造的715125字专属知识库,囊括了(但不限于)从职场、技术、管理与商业等多个板块的内容。

  • 一个ChatGPT触达不到的地方
  • 一个带你超越AI/人工智能的地方
  • 一个与你一起成长的地方

https://afoo.me/kb.html


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

订阅「福报」