考虑下面的记录定义和相应的方法:使用可选参数调用记录部件使用命名参数
type MyRecord = {
FieldA : int
FieldB : int
FieldC : int option
FieldD : int option
} with
static member Create(a,b,?c,?d) = {
FieldA = a
FieldB = b
FieldC = c
FieldD = d
}
调用Create方法如下成功:
//ok
let r1 = MyRecord.Create(1, 2)
//ok
let r2 = MyRecord.Create(1,2,3)
试图使用命名的参数,无论是具有必需或可选参数,但不会编译。对于根据MSDN文档(http://msdn.microsoft.com/en-us/library/dd233213.aspx)
命名参数只允许为方法,而不是让结合的功能,函数值,或lambda表达式示例
//Compilation fails with a message indicating Create requires four arguments let r2 = MyRecord.Create(FieldA = 1, FieldB =2)
。
因此,基于此,我应该能够使用命名参数来执行Create。我的语法有问题吗?还是我错误地解释了规则?有没有在这种情况下使用命名参数的方法?