constructor - How to define a ctor for a struct in F# that in turn calls the struct's default ctor -
how define ctor immutable struct in f#, accepts of fields. or, compared c# how zero out struct (like calling this() in c# example below) in f# ?
c#
struct point { private readonly int _x; private readonly int _y; public point(int x) : this() // 0 struct { _x = x; } public point(int y) : this() // 0 struct { _y = y; } }
the point(x) ctor above zeros out struct calling this() , sets single field. after. following example simplified, question how 0 struct , set single field.
f#
type point = struct val x: float val y: float new(x: float) = ? // how 0 out struct , set x ? end
i think f# version of c# code this
[<struct>] type point = val mutable x: float val mutable y: float let p = point(x = 1.0)
although struct members mutable, p
not cannot set members again.
// cause compile error p.x <- 2.0
Comments
Post a Comment