Реализация Ilist в F #
-
22-12-2019 - |
Вопрос
Я посмотрел по всему и интернет, но все равно не может выяснить, что мне не хватает, и как реализовать универсальный интерфейс Ilist в 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()
.
Сейчас компилятор жалуется, что я не реализовал коллекции. Pienableable.getenumerator () И я это знаю, но я действительно не знаю, как это сделать.
<Сильное> Обновление: Окончательный результат ниже.Большое благодаря г-ну Гирлянию.Кроме того, стоит указать, что я ссылался только только System.Collections.generic и забыл спрашивать System.collection, когда нерегированная версия Ienumerable находится.Так грустный ресхарпер не поддерживает 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
. Решение
Это связано с тем, что IList
имеет два метода GetEnumerator
, один для интерфейса IEnumerable<T>
и один для интерфейса бессмысленного генеракодицетагкода.
Вы можете добавить этот член для реализации IEnumerable
member this.GetEnumerator() = (this.Conditions :> IEnumerable).GetEnumerator()
. Другие советы
Самый простой способ -
interface System.Collections.IEnumerable with
member this.GetEnumerator() = this.Conditions.GetEnumerator() :> _
.