为什么变量在模式中绑定两次?

Mo *_* B. 17 f# pattern-matching

通常,F#不允许模式变量在同一模式中出现两次.但是在下面的代码中,F#没有抱怨 - 这是一个错误吗?

type foo = { A: int }
let test (x, {A = x}) = x
let result = test ("bla", {A = 3})
Run Code Online (Sandbox Code Playgroud)

Mar*_*cus 5

我不认为这是一个错误,虽然它似乎令人困惑,但它只是编译器编译上述值绑定的方式.

第14.6.3节规定,如果值定义不是单个值模式(如此处所示),则详细说明的表达式为

tmp <typars1… typarsn> = expr
ident1 <typars1> = expr1
…
identn <typarsn> = exprn
Run Code Online (Sandbox Code Playgroud)

其中tmp是一个新的标识符,每个expri都是根据模式pat(§7)对输入tmp [from spec ] 进行编译得到的.

要查看编​​译器的功能,我们可以使用F#引用,例如:

<@ let test (x:int, {A = x:int})= x in test (3, {A = 5}) @>;;
Run Code Online (Sandbox Code Playgroud)

在字符串格式中我得到(留下一些东西):

Let (test,
     Lambda (tupledArg,
             Let (x, TupleGet (tupledArg, 0),
                  Let (_arg1, TupleGet (tupledArg, 1),
                       Let (x, PropertyGet (Some (_arg1), A, []), x)))),
     Application (test, NewTuple (Value (3), NewRecord (foo, Value (5)))))
Run Code Online (Sandbox Code Playgroud)

这就是我们获得"内在"x的原因.

我不认为这是一个错误,但如果这会产生警告,也许会更加一致.