这种设计模式有名字吗?
我不断发现自己正在解决类似于钻石继承问题的问题(但没有任何继承!),如下所示:
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
类型 a
和 b
之间的共性通过 I<_>
接口公开,但接口的成员可以返回特定类型的值底层类型 a
或 b
而不必泛化为实现该接口的任何类型。
例如,这会返回 a
: 类型的值,
> (A :> I<_>).Foo;;
val it : a = A
而这会返回 b
: 类型的值,
> (B :> I<_>).Foo;;
val it : b = B
即使这些值已向上转换为接口类型。
这个有名字吗?其他人也在这样做吗?
I keep finding myself solving something similar to the diamond inheritance problem (but without any inheritance!) as follows:
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
The commonality between the types a
and b
is exposed via the I<_>
interface but members of the interface can return values of the specific underlying types a
or b
rather than having to be generalizing to any type implementing the interface.
For example, this returns a value of the type a
:
> (A :> I<_>).Foo;;
val it : a = A
and this returns a value of the type b
:
> (B :> I<_>).Foo;;
val it : b = B
even though the values were upcast to the interface type.
Is there a name for this? Are other people doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 C++ 中,这称为奇怪的重复模板模式。
In C++, this is called the Curiously recurring template pattern.