我到處都找過SO和互聯網,但仍無法找出什麼我失蹤,如何實現在F#泛型IList接口這樣的類型:實施的IList <T>在F#
type OrCondition() as self =
inherit Condition()
member val Conditions: List<Condition> = new List<Condition>() with get, set
interface IList<Condition> with
member this.Item
with get(index) = self.Conditions.[index]
and set(index)(value) = self.Conditions.[index] <- value
member this.IndexOf item = self.Conditions.IndexOf(item)
member this.Insert(index, item) = self.Conditions.Insert(index, item)
member this.RemoveAt(index) = self.Conditions.RemoveAt(index)
member this.Count with get() = self.Conditions.Count
member this.IsReadOnly with get() = false
member this.Add(item) = self.Conditions.Add(item)
member this.Clear() = self.Conditions.Clear()
member this.Contains(item) = self.Conditions.Contains(item)
member this.CopyTo(conditions, index) = self.Conditions.CopyTo(conditions, index)
member this.Remove(item) = self.Conditions.Remove(item)
member this.GetEnumerator() = (Seq.cast<Condition> self.Conditions).GetEnumerator()
眼下編譯器抱怨我沒有實現Collections.IEnumerable.GetEnumerator(),我知道它,但我真的不知道該怎麼做。
更新:下面的最終結果。非常感謝Garland先生。另外,值得指出的是,當引用IEnumerable的非泛型版本時,我只引用了System.Collections.Generic並忘記引用System.Collection。所以傷心的ReSharper不支持F#。
open System.Collections
open System.Collections.Generic
type OrCondition() as self =
inherit Condition()
member val Conditions = new List<Condition>() with get, set
interface IList<Condition> with
member this.Item
with get(index) = self.Conditions.[index]
and set(index)(value) = self.Conditions.[index] <- value
member this.IndexOf item = self.Conditions.IndexOf(item)
member this.Insert(index, item) = self.Conditions.Insert(index, item)
member this.RemoveAt(index) = self.Conditions.RemoveAt(index)
member this.Count with get() = self.Conditions.Count
member this.IsReadOnly with get() = false
member this.Add(item) = self.Conditions.Add(item)
member this.Clear() = self.Conditions.Clear()
member this.Contains(item) = self.Conditions.Contains(item)
member this.CopyTo(conditions, index) = self.Conditions.CopyTo(conditions, index)
member this.Remove(item) = self.Conditions.Remove(item)
member this.GetEnumerator() = self.Conditions.GetEnumerator() :> IEnumerator<Condition>
member this.GetEnumerator() = self.Conditions.GetEnumerator() :> IEnumerator
https://github.com/fsharp/fsharpx/blob/master/src/FSharpx.Collections 。實驗/ DList.fs#L71 –