“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 {
= 12345
port = 250.milliseconds
timeout }
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()
.start() server
Now it’s done!
Neat and cool, typesafe-proof configuration with scala as configuration DSL! Don’t you like it?! ^_^
References
欢迎加入「福强私学」
跨越2190个日夜,始终坚持“实践 + 原创”打造的715125字专属知识库,囊括了(但不限于)从职场、技术、管理与商业等多个板块的内容。
- 一个ChatGPT触达不到的地方
- 一个带你超越AI/人工智能的地方
- 一个与你一起成长的地方
开天窗,拉认知,订阅「福报」,即刻拥有自己的全模态人工智能。