您可以在模块中初始化私有值,然后再调用另一个函数来读取值吗?我虽然得到一个空字符串。
数据/ Credentials.fs
type Credentials = {
mutable clientId: string;
}
Authentication.fs
module Authentication =
let private credentials = {
clientId = "old";
}
let init (claims: Credentials) =
credentials.clientId <- claims.clientId // updating value
let requestToken =
printfn "reading %s\n" credentials.clientId // reading updated value
AuthenticationTest.fs
let credentials = {
clientId = "new";
}
init credentials // set credentials
requestToken // read credentials
预期输出:
阅读新内容
实际输出:
读旧
答案 0 :(得分:2)
requestToken
被定义为变量,其中包含printfn "reading %s\n" credentials.clientId
的结果。你想要的可能是
let requestToken () =
printfn "reading %s\n" credentials.clientId
...
requestToken ()