【发布时间】:2018-11-04 20:00:14
【问题描述】:
我正在尝试在我的代码中实现 F# coding conventions 页面中的出色建议
https://docs.microsoft.com/en-us/dotnet/fsharp/style-guide/conventions.
Use classes to contain values that have side effects 部分特别有趣。它说
There are many times when initializing a value can have side effects, such as instantiating a context to a database or other remote resource. It is tempting to initialize such things in a module and use it in subsequent functions.
并提供了一个例子。然后指出了这种做法的三个问题(由于篇幅不足我省略了,但可以在链接的文章中看到)并建议使用简单的类来保存依赖关系。
我想知道应该如何对待类型提供程序?例如,如果我有以下代码,
[<Literal>]
let projDataPath = __SOURCE_DIRECTORY__ + @"\data\"
[<Literal>]
let configPath = projDataPath + "config.json"
type Cnfg = JsonProvider<Sample=configPath>
let config = Cnfg.Load(configPath)
使用类型提供程序来初始化值是否会遇到与值初始化相关的问题以及文章中描述的副作用?
换句话说,我应该将类型提供程序包装在一个类中吗?
【问题讨论】:
标签: class f# type-providers side-effects