Question

I have looked all over SO and internet but still cannot figure out what I'm missing and how to implement generic IList interface in F# for such type:

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

Right now compiler complains that I haven't implemented Collections.IEnumerable.GetEnumerator() and I know it, but I really don't know how to do it.

Update: Final result below. Great thanks to Mr. Garland. Also, worth to point out that I have referenced only System.Collections.Generic and forgot to reference System.Collection when non-generic version of IEnumerable reside. So sad ReSharper doesn't support 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 
Was it helpful?

Solution

This is because IList has two GetEnumerator methods, one for the IEnumerable<T> interface and one for the non-generic IEnumerable interface.

You could add this member to implement IEnumerable

member this.GetEnumerator() = (this.Conditions :> IEnumerable).GetEnumerator()

OTHER TIPS

The simplest way is

interface System.Collections.IEnumerable with
    member this.GetEnumerator() = this.Conditions.GetEnumerator() :> _
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top