王凯越 Kaiyue Wang
王凯越 Kaiyue Wang

Reputation: 694

How is this parameter in constructor used in member method directly in FSharp?

This example (counter example) is given in the "Removing Cyclic Dependencies" of FSharp for fun and profit. 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

See the parameter observer in the Customer class. It is not declared as a field or property in the type. What is it then?

Upvotes: 1

Views: 93

Answers (1)

Devon Burriss
Devon Burriss

Reputation: 2532

In F#, args from the constructor are in scope throughout the class declaration. This differs from C# and VB.

It is mentioned in the documentation here.

The arguments of the primary constructor are in scope throughout the class declaration.

Upvotes: 5

Related Questions