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())
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:
let mkEnum<'T> (ie : System.Collections.IEnumerable) = (Seq.cast<'T> ie).GetEnumerator()
I think your way looks best. I guess this is more succinct (I have not tried it end-to-end to ensure it works):
let mkEnum<'T>(ie : System.Collections.IEnumerable)
: System.Collections.Generic.IEnumerator<'T> =
(seq { for x in ie -> unbox x }).GetEnumerator()
but I wonder if it allocates more.
As is pointed out by @desco, this is even shorter:
let mkEnum<'T> ie = (Seq.cast<'T>(ie)).GetEnumerator()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With