如何在FSharp的成员方法中直接使用构造函数中的此参数?

时间:2019-03-02 01:27:19

标签: .net oop functional-programming f# .net-core

此示例(计数器示例)在FSharp的“删除循环依赖项”中给出了乐趣和收益。 https://fsharpforfunandprofit.com/posts/removing-cyclic-dependencies/

type Customer(name, observer:CustomerObserver) = 
        let mutable name = name
        member this.Name 
            with get() = name
            and set(value) = 
                name <- value
                observer.OnNameChanged(this)

and CustomerObserver() = 
    member this.OnNameChanged(c:Customer) =     
        printfn "Customer name changed to '%s' " c.Name

请参阅observer类中的参数Customer。它不会在类型中声明为字段或属性。之后怎么样了?

1 个答案:

答案 0 :(得分:5)

在F#中,整个类声明中,构造函数的args都在作用域内。这与C#和VB不同。

它在documentation here中有所提及。

  

在整个类声明中,主构造方法的参数都在范围内。