编译器如何处理扩展记录

时间:2012-07-23 18:53:52

标签: f#

当扩展记录(深拷贝?)时,编译器是否在内存中创建了一个新位置?或者编译器是否使记录可变并修改了值?

例如:

type MyRecord = { A : string
                ; B : string
                }

let record = { A = "A"; B = "B" }

let record = { record with A = "new A" } //copy or overwrite? 

由于我覆盖record,编译器是否会复制或覆盖?是否存在性能问题?

2 个答案:

答案 0 :(得分:6)

它制作副本。

Copy-and-update Record expression

*复制和更新记录表达式详细说明它是如下所示的记录表达式:

让{= label1 = expr1;中的v = expr; ......; field-labeln = exprn; F1 = v.F1; ......; FM = v.FM}

其中F1 ... FM是R的字段,未在字段初始值设定项中定义,v是新变量。*

答案 1 :(得分:2)

type T = {
  A : string
  B : string
}

let x = { A = "a"; B = "b" }
let y = { x with A = "aa" }

等同于此

class T {
    public readonly string A;
    public readonly string B;
    public T(string a, string b) {
        A = a;
        B = b;
    }
}

var x = new T("a", "b");
var y = new T("aa", x.B);