我想创建自己的自定义集合类型。
我将集合定义为:
type A(collection : seq<string>) =
member this.Collection with get() = collection
interface seq<string> with
member this.GetEnumerator() = this.Collection.GetEnumerator()
但这不会编译
No implementation was given for 'Collections.IEnumerable.GetEnumerator()
我该怎么做呢?
最佳答案
在F#中,seq
实际上只是System.Collections.Generic.IEnumerable<T>
的别名。通用IEnumerable<T>
也实现了非通用IEnumerable
,因此您的F#类型也必须这样做。
最简单的方法是仅将非通用的一个调用插入通用的一个
type A(collection : seq<string>) =
member this.Collection with get() = collection
interface System.Collections.Generic.IEnumerable<string> with
member this.GetEnumerator() =
this.Collection.GetEnumerator()
interface System.Collections.IEnumerable with
member this.GetEnumerator() =
upcast this.Collection.GetEnumerator()
关于f# - 从Seq继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10096693/