嵌套通用类型的接口

时间:2018-05-22 07:50:54

标签: generics f# nested-generics

给出以下界面:

type A<'t> =
    abstract Scratch: 't

如何简单地创建这样的界面:

type X<A<'t>> =
    abstract f: A<'t>

我也试过这个:

type X<'t,'y when 'y:A<'t>> =
    abstract f: 'y

和此:

type X<'t,A<'t>> =
    abstract f: A<'t>

但没有一个适合我。

1 个答案:

答案 0 :(得分:5)

如果你想以界面的方式试试这个:

type IA<'a> =
    abstract Scratch: 'a

type IX<'a, 'b when 'a :> IA<'b>> = 
    abstract F: 'a

type RealA() = 
    interface IA<int> with
        member __.Scratch = 1

type RealX = 
    interface IX<RealA, int> with
        member __.F = RealA()

如果你想要抽象类:

[<AbstractClass>]
type A<'a>() = 
    abstract member Scratch: 'a

[<AbstractClass>]
type X<'a, 'b when 'a :> A<'b>>() = 
    abstract F: 'a

type RealA'() = 
    inherit A<int>()
    override __.Scratch = 1

type RealX'() = 
    inherit X<RealA', int>()
    override __.F = RealA'()