Pregunta

He mirado por todas partes, de MODO y de internet, pero todavía no puede averiguar lo que me falta y cómo implementar genérico interfaz IList en F# para ese tipo:

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

Ahora compilador se queja de que no he implementado Colecciones.IEnumerable.GetEnumerator() y yo lo sé, pero yo realmente no sé cómo hacerlo.

Actualización: Resultado Final a continuación.Un gran agradecimiento para el Sr.Garland.También, vale la pena señalar que la tengo hace referencia sólo Sistema.Las colecciones.Genérico y se olvidó de Sistema de referencia.Colección cuando no genérica versión de IEnumerable residen.Tan triste ReSharper no apoyo 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 
¿Fue útil?

Solución

Esto es debido a que IList tiene dos GetEnumerator métodos, uno para el IEnumerable<T> interfaz y uno de los no-genérico IEnumerable la interfaz.

Usted podría agregar a este miembro a implementar IEnumerable

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

Otros consejos

La forma más sencilla es

interface System.Collections.IEnumerable with
    member this.GetEnumerator() = this.Conditions.GetEnumerator() :> _

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top