f# - 从Seq继承

标签 f#

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

我将集合定义为:

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/

相关文章:

arrays - F# 合并排序 - 尝试实现与结构的匹配时出现 IndexOutOfRangeException

c# - 在 Azure Functions 中发出故障信号的最佳方式是什么?

azure - #load 无法加载共享 .fsx

oop - 如何将 Haskell 类型类转换为 F#?

f# - FParsec:如何组合解析器以便它们以任意顺序匹配

f# - FSharp.Data : Transform multiple columns to a single column (dictionary result)

c# - 在 CoreCLR 中运行 F#

f# - 如何遍历联合案例列表并访问每个案例的数据?

arrays - F# 数组平面图

performance - F# 函数可以被视为尾递归吗?它使用 TailCall .net 操作码