我的类型中的 F# 结构化结构不完整

F# incomplete structured construct in my type

我正在尝试在 F# 中实现一个 OrderedSet(您可以在其中按插入顺序获取项目的集合)。这是我第一次天真的尝试(我仍然必须处理传递给构造函数的 IEnumerable 中可能的重复项。比较器也仍然缺失)。

不过,我认为以上任何一个都不重要。我一直在努力解决这个问题,据我从研究中收集到的信息,问题似乎与缩进或某处缺少括号有关。

如果我注释掉我对实现 ISet 的尝试,没有问题,这让我相信那部分特别有问题。这是代码:

open System
open System.Collections.Generic;

type public OrderedSet<'T> public (collection : IEnumerable<'T> option) as this =
    let mutable _set = SortedSet<int * 'T>()

    do
    if Option.isSome(collection) then
        let mapper (idx: int) (elem: 'T) = (idx, elem)
        do _set <- SortedSet<int * 'T>(Seq.mapi mapper collection.Value)

    interface ISet<'T> with
        member this.Count
            with get() = this._set.Count

我只需要进行两个更改:

  • do 下缩进 if
  • 删除_set前面的this.,因为它是let绑定的,不是class成员。

当然,您还必须完成实施 ISet。这是生成的代码:

open System
open System.Collections.Generic;

type public OrderedSet<'T> public (collection : IEnumerable<'T> option) as this =
    let mutable _set = SortedSet<int * 'T>()

    do
        if Option.isSome(collection) then
            let mapper (idx: int) (elem: 'T) = (idx, elem)
            do _set <- SortedSet<int * 'T>(Seq.mapi mapper collection.Value)

    interface ISet<'T> with
        member this.Count
            with get() = _set.Count