代码之家  ›  专栏  ›  技术社区  ›  Mohsen

嵌套的通用接口类型

  •  3
  • Mohsen  · 技术社区  · 8 年前

    给定以下接口:

    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 回复  |  直到 8 年前
        1
  •  5
  •   Szer    8 年前

    如果要将其作为接口执行,请尝试以下操作:

    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'()