[英]Trouble with a member constraint invocation expression when the member signature has type variables in F#
当成员签名具有类型变量时,我在使用成员约束调用表达式时遇到麻烦。 具体来说,代码
[<AutoOpen>]
module Foo
// Preapplied lens
type lens <'i,'j> = { get : 'j; set : 'j -> 'i }
// Record type with lenses
type foo1 = {
num_ : int;
letter_ : char
} with
member this.num = {
get = this.num_;
set = (fun num' -> {this with num_=num'})}
member this.letter = {
get = this.letter_;
set = (fun letter' -> {this with letter_=letter'})}
end
let (foo1:foo1) = {num_ = 1; letter_ = 'a'}
// Another ecord type with lenses
type foo2 = {
num_ : int;
name_ : string
} with
member this.num = {
get = this.num_;
set = (fun num' -> {this with num_=num'})}
member this.name = {
get = this.name_;
set = (fun name' -> {this with name_=name'})}
end
let (foo2:foo2) = {num_ = 2; name_ = "bob"}
// Add two to any record with the num lens
let inline add2 (x : ^t) =
let num = (^t : (member num : lens<^t,int>) (x)) in
num.get + 2
给出错误:
test05.fsx(39,47):表达式中错误FS0010:意外符号
)
问题出在倒数第二行的成员约束调用表达式上。 基本上,我不知道如何在成员约束调用表达式中将类型变量^t
和int
传递给lens
类型。 有一个好的方法可以做到这一点吗?
静态约束中<
和^
之间必须有一个空格:
// Add two to any record with the num lens
let inline add2 (x : ^t) =
let num = (^t : (member num : lens< ^t,int>) (x)) in
num.get + 2
否则,两个字符<^
将被视为中缀函数的名称(也称为“运算符”)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.