【问题标题】:F# LazyList -> IListF# LazyList -> IList
【发布时间】:2013-11-07 10:58:20
【问题描述】:

我正在创建一个 C# 代码将使用的模块,所以我宁愿不直接返回 LazyList<>。如果我这样做了,C# 将不得不引用FSharp.PowerPack,这看起来很奇怪。

所以我宁愿返回IList<>,但是当我尝试做类似的事情时:

let x = LazyList.ofSeq {0..10}
let y = x :> IList<int32>

但是,这给了我错误:

The type 'LazyList<int> is not compatible with the type 'IList<int>'

这让我相信LazyList&lt;&gt; 不是implement IList&lt;&gt;。这是真的吗?

我错过了什么?

【问题讨论】:

  • 这个post 可能会有所帮助
  • IList&lt;_&gt; 公开了诸如 Count 之类的内容,这仅在列表完全构建时才有意义,因此 LazyList&lt;_&gt; 没有实现该接口是有意义的。
  • @kvb:我接受了 DoctorJones (stackoverflow.com/questions/2689986/…) 的话……但它无法完成 IList&lt;&gt; 的大部分功能绝对是有道理的
  • 我认为这实际上是指不同的 LazyList&lt;_&gt; 类,而不是 F#。
  • ExtCore 库中有LazyList,它也实现IList (github.com/jack-pappas/ExtCore/blob/master/ExtCore/…),但我认为这是一个合理的补充,所以也许你可以提交一个拉取请求并使用它?

标签: f# lazylist


【解决方案1】:

确实,LazyList 没有实现 IList 接口。不过它实现了 IEnumerable。

[<Sealed>]
type LazyList<'T> =
   interface IEnumerable<'T>
   interface System.Collections.IEnumerable

【讨论】:

    猜你喜欢
    • 2014-08-27
    • 1970-01-01
    • 1970-01-01
    • 2012-06-27
    • 2019-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-30
    相关资源
    最近更新 更多