如何创建实现IDictionary<'K,'V>的类型.和IEnumerable<'V> [英] How to create a type that implement IDictionary<'K, 'V> and IEnumerable<'V>
问题描述
我想创建一个实现IDictionary <'K,'V>和IEnumerable <'V>的只读键集合.采用明显的方法,我得到以下错误:
I want to create a read-only keyed collection that implements IDictionary<'K, 'V> and IEnumerable<'V>. Taking the obvious approach I get the following error:
此类型在不同的通用实例"IEnumerable<'V>"上实现或继承相同的接口和"IEnumerable< KeyValuePair<'K,'V>".在此版本的F#中不允许这样做.
有其他方法可以实现这一目标吗?
Is there a different way of achieving this?
编辑-由于这似乎是F#的不可逾越的局限性,实现此目标的惯用方式是什么?想到的一个想法是提供返回数据所需视图的成员,例如,成员x.List:IList <'V>和成员x.Dict:IDictionary <'K,'V>.对象表达式可用于提供实现.还有其他想法吗?
EDIT - Since this seems to be an insurmountable limitation of F#, what would be an idiomatic way of achieving this? One thought that comes to mind is providing members that return the desired view of the data, e.g., member x.List : IList<'V> and member x.Dict : IDictionary<'K, 'V>. Object expressions could be used to provide the implementations. Any other ideas?
推荐答案
恐怕不是.CLR允许实现多个接口(当然,甚至是相同的基本类型),但不能实现F#语言.我相信,如果您使用C#编写类,不会有任何问题,但是F#在当前版本中会给您带来问题.
I'm afraid not. The CLR allows implementation of multiple interfaces of course (even of the same base type), but not the F# language. I believe you won't have any problems if you write the class in C#, but F# is going to give you problems in the current version.
这篇关于如何创建实现IDictionary<'K,'V>的类型.和IEnumerable<'V>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!