I get the next errors with my source:
- 'Name' hasn't value
- You should call an alternate constructor
Then, how do I the next class in F#?
type Person = class
member val Name: string
member val Age: int option = None
new() = { Name = "Anonymous" }
new(name) = { Name = name }
new(name, age: int) = { Name = name; Age = age }
static member Hello =
printfn "Hello, %O" Name
end
Edit
Based on the @user2088029 answer in this post and the @kvb answer in Object initializer + property initializer (from C# to F#), I have created the next source, which has I want get. The problem is "Person(name, Age = age)" indicates error with "No accessible member or object constructor named 'Person' takes 1 argument".
type Person(name) =
member this.Name: string = name
member this.Age: int option = None //with get,set
new() = Person("Anonymous")
new(name, age: int) = Person(name, Age = age)
member this.Hello =
printfn "Hello, %O" this.Name
Usage:
let person = Person("Alex", Option = Some 25)