Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing IEnumerable<'T> when base class implements IEnumerable

Tags:

ienumerable

f#

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()
like image 666
Daniel Avatar asked Dec 08 '11 21:12

Daniel


2 Answers

shorter version of Brian's suggestion:

let mkEnum<'T> (ie : System.Collections.IEnumerable) = (Seq.cast<'T> ie).GetEnumerator()
like image 119
desco Avatar answered Dec 31 '22 19:12

desco


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()
like image 30
Brian Avatar answered Dec 31 '22 19:12

Brian