I am trying to implement a ToEnumerable property on an external type that does not implement it. I can’t quite get the code to work.
So I dumbed it down to adding a untyped GetEnumerator property, and added the code for ToComparable for guidance. However, I have no idea on how to store the mutable state for the counter.
Is the pb the anonymous class ?
How would you do it ?
open System
open System.Collections
open System.Collections.Generic
type Bloomberglp.Blpapi.Element with
//**WORKS OK**
member this.ToComparable:IComparer<Bloomberglp.Blpapi.Element> = {
new IComparer<Bloomberglp.Blpapi.Element> with
member this.Compare(x, y) = x.NumValues.CompareTo(y.NumValues)
}
//**WORKS (sort of) OK without storing the state**
member this.GetEnumerator2:IEnumerator = {
//let mutable i =0
new IEnumerator with
member this2.Reset() =
i <- 0;
()
member this2.MoveNext() =
if i < n then
i <- i + 1
true
else
false
member this2.Current
with get() =
this.GetElement(0) :> obj
}
Assuming
NumValuesis the count, you could do something like this:This returns
IEnumerator<'T>where'Tis the return type ofGetElement.