6

我想创建自己的自定义集合类型。

我将我的收藏定义为:

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()

我该怎么做呢?

4

1 回答 1

13

在 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()
于 2012-04-10T21:32:26.207 回答