Object在拆箱时会在栈空间生成一个临时变量。所以Struct在使用时尽量将内容都声明为readonly为好
[<Struct>] type Point= val mutable X:double val mutable Y:double member x.Change(a, b) = x.X <- a x.Y <- b override x.ToString() = sprintf "%A %A" x.X x.Y let mutable p = Point(X=1., Y=1.) p.ToString()//out: 1.0 1.0 p.Change(3., 2.) p.ToString()//out: 3.0 2.0 let o = p :> obj o.ToString()//out: 3.0 2.0 (o :?> Point).Change(5., 5.) o.ToString()//no change out: 3.0 2.0
f# 生成的struct实例默认是只读的 也就是说不加上mutable, Change压根就不起作用。保护的更好一些。