采用这篇文章在 F# 中实现幻像类型中解释的幻像类型的想法,我试图限制抽象类型成员的输入参数,更一般地说是任何类型成员。这可能是不可能的,我很高兴收到这个信息。
这是我正在尝试的:
// my phantom type
type Ascending = interface end
type Descending = interface end
type HeapOrder =
inherit Ascending
inherit Descending
// my type
type IHeap<'a when 'a : comparison> =
inherit System.Collections.IEnumerable
inherit System.Collections.Generic.IEnumerable<'a>
...
abstract member Order : HeapOrder with get
...
// ...and one or the other of these inherited types :
type IHeap<'c, 'a when 'c :> IHeap<'c, 'a> and 'a : comparison> =
inherit IHeap<'a>
// or:
type IHeap<'a, 'd when 'a : comparison and 'd :> HeapOrder> =
inherit System.Collections.IEnumerable
inherit System.Collections.Generic.IEnumerable<'a>
type IHeap<'c, 'a, 'd when 'c :> IHeap<'c, 'a, 'd> and 'a : comparison and 'd :> HeapOrder> =
inherit IHeap<'a>
...
// ...and the member I want to constrain, so that the HeapOrder of the
// input parameter matches the HeapOrder of the instantiated type.
// Conceptually one of these alternatives (none of these build,
// but they should convey what I'm attempting):
//
abstract member Merge : ('c when 'c<'c, 'a> : (member Order when Order :> 'd))
// or
abstract member Merge : ('c when c' :> IHeap<'c, 'a, 'd>) -> 'c
// or
abstract member Merge : ('c when c' : #IHeap<'c, 'a, 'd>) -> 'c