如何从成员函数分配给类的成员变量

时间:2016-09-25 23:15:09

标签: f#

对F#来说很新。我有一个这样的课程

namespace FsharpTestSystem

open System
open System.IO
open System.Text
open System.Diagnostics

open System.CodeDom
open Microsoft.FSharp.Compiler.SourceCodeServices
open Microsoft.FSharp.Compiler.Interactive.Shell
open Microsoft.FSharp.Compiler.SimpleSourceCodeServices

type TestSystem() =
// Intialize output and input streams
// Build command line arguments & start FSI session

    let argv = [| "C:\\Program Files (x86)\\Microsoft SDKs\\F#\\4.1\\Framework\\v4.0\\fsi.exe" |]
    let allArgs = Array.append argv [|"--noninteractive"|]

    let sbOut = new StringBuilder()
    let sbErr = new StringBuilder()
    let inStream = new StringReader("")
    let outStream = new StringWriter(sbOut)
    let errStream = new StringWriter(sbErr)
    let fsiConfig = FsiEvaluationSession.GetDefaultConfiguration() 


....

我在这个类中添加了一个成员方法(函数)。但是,如何将返回值分配给类的成员变量,以便其他成员可以看到?换句话说,fsis

的其他成员需要使用TestSystem

在C#中,这是微不足道的,例如通过使用属性。我怀疑我需要告诉F#系统fsis在声明它时会以某种方式变异。

member this.LoadFSI() =
    fsis = FsiEvaluationSession.Create(fsiConfig, allArgs, inStream, outStream, errStream)

member this.UseFSI() =
     //I need to use fsis in here.

1 个答案:

答案 0 :(得分:0)

你确实生活在最前沿......这个怎么样:

type TestSystem()  =
    // ....
    [<DefaultValue>] val mutable Fsis : FsiEvaluationSession
    member __.LoadFsis() = __.Fsis <- FsiEvaluationSession.Create(fsiConfig, allArgs, inStream, outStream, errStream)
    member __.UseFsis() = __.Fsis.GetHashCode()


let ts = TestSystem()
ts.LoadFsis()
ts.UseFsis()
ts.Fsis.GetHashCode()
ts.LoadFsis()
ts.UseFsis()

从哈希码(我假设)中你可以看到,只要你不做.LoadFsis它就是使用相同的会话。我相信这可以做得更漂亮。