genericsf#nested-generics

Nested generic types of interfaces


Given the following interface:

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

How to simply create an interface like this:

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

I also tried this one:

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

and this:

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

but none work for me.


Solution

  • If you want to do it as interface try this:

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

    If you want abstract classes:

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