统一时具有无限类型约束的f#接口
我在获取具有类型约束的接口以使其正常工作时遇到问题 这是类型统一时具有无限类型约束的f#接口,f#,F#,我在获取具有类型约束的接口以使其正常工作时遇到问题 这是类型 type LeftistHeap<'a when 'a : comparison> = ... interface IHeap<LeftistHeap<'a>, 'a> with ... member this.Insert (x : 'a) = LeftistHeap.insert x this type LeftistHeap= ... 与IHeap的接口 ... 成员
type LeftistHeap<'a when 'a : comparison> =
...
interface IHeap<LeftistHeap<'a>, 'a> with
...
member this.Insert (x : 'a) = LeftistHeap.insert x this
type LeftistHeap=
...
与IHeap的接口
...
成员this.Insert(x:'a)=LeftistHeap.Insert x this
以及界面
type IHeap<'a when 'a : comparison> =
inherit System.Collections.IEnumerable
inherit System.Collections.Generic.IEnumerable<'a>
...
type IHeap<'c, 'a when 'c :> IHeap<'c, 'a> and 'a : comparison> =
inherit IHeap<'a>
...
abstract member Insert : 'a -> 'c
let insertThruList l h =
List.fold (fun (h' : IHeap<_,'a>) x -> h'.Insert x ) h l
类型IHeap=
继承System.Collections.IEnumerable
继承System.Collections.Generic.IEnumerable IHeap和'a:比较>=
继承IHeap'c
这段代码没问题
let insertThruList l h =
List.fold (fun (h' : LeftistHeap<'a>) x -> h'.Insert x ) h l
让insertThruList l h=
List.fold(fun(h):LeftistHeap)x->h'.插入x)hl
我在h.Insert处得到这个错误
类型不匹配。期待
“b但是给一个 IHeap
当统一“b”和“IHeap”时,结果类型将是无限的
编译器是对的:您试图在需要
IHeap-IHeap h的地方使用'c
。插入x:>\uh l
或者,您可以表示不希望输入(确切地说)是一个IHeap
,而是某些特定的子类型:
let insertThruList l h =
List.fold (fun (h' : #IHeap<_,_>) x -> h'.Insert x) h l
让insertThruList l h=
List.fold(fun(h':#IHeap)x->h'.插入x)hl
这可能是您真正想要的(类型更具体)。这相当于更详细的定义:
let insertThruList<'c,'a when 'a : comparison and 'c :> IHeap<'c,'a>> l h =
List.fold (fun (h' : 'c) x -> h'.Insert x) h l
let insertThruList IHeap>l h=
List.fold(fun(h':'c)x->h'。插入x)hl
这对您的案例有效吗
let insertThruList l (h : 'T when 'T :> IHeap<'T, 'a> ) =
List.fold (fun (h' : 'T) x -> h'.Insert x ) h l
let insertThruList l(h:'T when'T:>IHeap)=
List.fold(fun(h':'T)x->h'。插入x)hl
柔性指示灯出现在紧急情况下。List.fold(fun(h'):#IHeap进入下一个问题。当约束类型接口是元组的一部分时,这似乎不起作用。例如:(fun(x:(#IHeap Same“infinte when unifing”问题。@Jack-你能给出一个更完整的例子吗?我不明白为什么这样不行。我只是准备做一个精简的项目来复制这个问题。应该很快完成(有太多的中断)。我将问题隔离到一个取消装箱的问题,让x1=box(元组l)让x2:#IHeap*list
let insertThruList l (h : 'T when 'T :> IHeap<'T, 'a> ) =
List.fold (fun (h' : 'T) x -> h'.Insert x ) h l