Is there a succinct way to implement IEnumerable<'T> in a type whose base type implements the non-generic IEnumerable? I’m using the mkEnumerator function below to do it, but it seems excessive.
open System
open System.Collections.Generic
open System.Configuration
let mkEnumerator<'T> (e : System.Collections.IEnumerator) =
{ new IEnumerator<'T> with
member __.Current = e.Current |> unbox
interface System.Collections.IEnumerator with
member __.Current = e.Current
member __.MoveNext() = e.MoveNext()
member __.Reset() = e.Reset()
interface IDisposable with
member __.Dispose() =
match e with
| :? IDisposable as d -> d.Dispose()
| _ -> () }
type MyConfigElement() =
inherit ConfigurationElement()
type MyConfigCollection() =
inherit ConfigurationElementCollection()
override __.CreateNewElement() = Unchecked.defaultof<_>
override __.GetElementKey(_) = Unchecked.defaultof<_>
interface IEnumerable<MyConfigElement> with
member __.GetEnumerator() = mkEnumerator<MyConfigElement> (base.GetEnumerator())
Update
Following desco’s suggestion, I’m now using:
type MyConfigCollection() =
inherit ConfigurationElementCollection()
...
interface IEnumerable<MyConfigElement> with
member __.GetEnumerator() = (Seq.cast<MyConfigElement> this).GetEnumerator()
shorter version of Brian’s suggestion: