私がそうするならそれは意味がありません
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