这个设计模式有名字吗?

时间:2010-11-11 11:03:27

标签: design-patterns interface types

我一直在发现自己解决类似于diamond inheritance problem(但没有任何继承!)的内容,如下所示:

  type I<'a> =
     abstract member Foo : 'a

  type a =
     | A

     interface I<a> with
        member this.Foo = this

  type b =
     | B

     interface I<b> with
        member this.Foo = this

ab类型之间的通用性通过I<_>接口公开,但接口成员可以返回特定基础类型a或{{ 1}}而不是必须推广到实现接口的任何类型。

例如,这会返回b类型的值:

a

并返回> (A :> I<_>).Foo;; val it : a = A 类型的值:

b

即使值已向上转换为接口类型。

这有名字吗?其他人这样做吗?

1 个答案:

答案 0 :(得分:0)

在C ++中,这被称为Curiously recurring template pattern