如果我这样做没有意义吗
type Point =
struct
val Row: int
val Column: int
new (row, column) = if row >= 0 && column >= 0 then { Row = row; Column = column }
else failwith "Cooridinators must be non-negative!"
// This is not a valid object construction
static member (+) (x: Point, y: Point) = Point (x.Row + y.Row, x.Column + y.Column)
static member (-) (x: Point, y: Point) = Point (x.Row - y.Row, x.Column - y.Column)
static member (*) (x: Point, a) = Point (x.Row * a, x.Column * a)
static member (*) (a, x: Point) = Point (x.Row * a, x.Column * a)
end
如果它是一个类,那么也许我可以在绑定期间引发异常do
,但是在结构中没有do
,我应该怎么做?
我发现可以在之后添加另一个构造函数failwith
来解决这个问题,但它提出了另一个问题,我怎样才能调用隐式构造函数?我必须先明确地构造它吗
new () = { Row = 0; Column = 0}
// Error structs auto supports a default constructor
如果我只是使用默认构造函数执行此操作
new (row, column) = if row >= 0 && column >= 0 then { Row = row; Column = column }
else
failwith "Cooridinators must be non-negative!"
new Point () // error
在我看来,Point ()
返回一个单位而不是Point
?