[英]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.