具有具体类型参数的泛型类型的F#类型扩展

时间:2020-02-07 09:52:48

标签: generics f# type-extension

是否可以使用具体的类型参数对泛型类型进行类型扩展?这些语法使我失败了:

语法1:

type Foo<Bar> with

    member this.BarStuff = // access `Bar´ specifics

语法2:

type Foo<'T when 'T :> Bar> with

   member this.BarStuff = // access `Bar´ specifics

1 个答案:

答案 0 :(得分:3)

是的,可以,但是您应该使用C#样式语法:

open System.Runtime.CompilerServices

type Bar = Bar
type Foo<'t> = Foo of 't

[<Extension;Sealed>]
type Extensions =
    [<Extension>]
    static member BarStuff (this: Foo<Bar>) = ()


let x = (Foo Bar).BarStuff
相关问题