“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

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

「为AI疯狂」星球上,扶墙老师正在和朋友们讨论有趣的AI话题,你要不要⼀起来呀?^-^
这里

  1. 不但有及时新鲜的AI资讯和深度探讨
  2. 还分享AI工具、产品方法和商业机会
  3. 更有体系化精品付费内容等着你,加入星球(https://t.zsxq.com/0dI3ZA0sL) 即可免费领取。(加入之后一定记得看置顶消息呀!)

知识星球二维码

存量的时代,省钱就是赚钱。
在增量的时代,省钱其实是亏钱。
避坑儿是省钱的一种形式,更是真正聪明人的选择!
弯路虽然也是路,但还是能少走就少走,背后都是高昂的试错成本。
订阅「福报」,少踩坑,少走弯路,多走一步,就是不一样的胜率!

订阅「福报Premium订阅」