Reputation: 493
I have a Person and I want initialize the Name with the property initializer and the Age with the constructor.
C# version
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person(int age)
{
Age = age
}
}
var person = new Person(20) { Name = "Alex" };
I've tried with F#:
Try 1: Invalid syntax
type Person = {
Name: string
Age: int
} with
static member create (age: int): Person =
{ this with Age = age }: Person
Try 2: Invalid syntax
type Person =
member val Name: string
member val Age: int
new(age: int)
this.Age = 13
Upvotes: 3
Views: 424
Reputation: 55184
Should be as simple as
type Person(age:int) =
member val Name = "" with get, set
member val Age = age with get, set
let person = Person(20, Name = "Alex")
Upvotes: 6