要解释我认为最好用一个例子:
type myRec = {x: string}
type myRec2 = {x: string}
let x = {x = "hello"}
let y(a: myRec) = a.x
y(x);;
y(x);;
--^
error FS0001: This expression was expected to have type
myRec
but here has type
myRec2
那么,如果x
和myRec
具有相同的签名,我如何强制myRec
拥有myRec2
类型?
答案 0 :(得分:11)
let x = { myRec.x = "hello" }
// or
let x:myRec = { x = "hello" }
// or
let x = { x = "hello" } : myRec
the documentation中提供了更多详细信息和示例。
编辑:注释中的备选方案。
答案 1 :(得分:2)
是的,你可以:
let x = { new myRec() with x = "hello" }
使用and
分配更多字段:
let x = { new myRec3() with x = "hello" and y = "bye" }