初始化私有变量以供后续使用

时间:2018-03-14 21:24:17

标签: f#

您可以在模块中初始化私有值,然后再调用另一个函数来读取值吗?我虽然得到一个空字符串。

数据/ 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

预期输出:

  

阅读新内容

实际输出:

  

读旧

1 个答案:

答案 0 :(得分:2)

requestToken被定义为变量,其中包含printfn "reading %s\n" credentials.clientId的结果。你想要的可能是

let requestToken () =
      printfn "reading %s\n" credentials.clientId
...
requestToken ()