所以我读过,如果你从一个 List 中获取一个类型结构,你会得到一个副本,因为 struct 是不可变的。这么说,如果你想做出改变,你必须替换列表中的整个结构。现在我用下面的代码经历了一个非常奇怪的行为
if pressed then
let ret = rectangles.Item(ret_index)
ret.Offset(loc.X - offs.Width - ret.X, loc.Y - offs.Height - ret.Y)
rectangles.Item(ret_index) <- ret
for i = 0 to couples.Count - 1 do
match couples.Item(i) with
| (idx_s, idx_e, rectf, string) when idx_s = ret_index ||
idx_e = ret_index ->
let mutable r = rectf
let x = (rectangles.Item(idx_e).X + rectangles.Item(idx_s).X) / 2
let y = (rectangles.Item(idx_e).Y + rectangles.Item(idx_s).Y) / 2
r.X <- single x
r.Y <- single y
couples.Item(i) <- (idx_s, idx_e, r, string)
那么定义可变矩形是不好的做法吗?(我读过可变结构是“邪恶的”)现在我真正的问题是:为什么上面的代码有效但下面的代码无效?
if pressed then
let ret = rectangles.Item(ret_index)
ret.Offset(loc.X - offs.Width - ret.X, loc.Y - offs.Height - ret.Y)
rectangles.Item(ret_index) <- ret
for i = 0 to couples.Count - 1 do
match couples.Item(i) with
| (idx_s, idx_e, rectf, string) when idx_s = ret_index ||
idx_e = ret_index ->
let r = rectf
let x = (rectangles.Item(idx_e).X + rectangles.Item(idx_s).X) / 2
let y = (rectangles.Item(idx_e).Y + rectangles.Item(idx_s).Y) / 2
r.Offset(single x - r.X, single y - r.Y)
couples.Item(i) <- (idx_s, idx_e, r, string)