Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inherit from Seq

Tags:

f#

I want to create my own custom collection type.

I define my collection as:

type A(collection : seq<string>) =
   member this.Collection with get() = collection

   interface seq<string> with
      member this.GetEnumerator() = this.Collection.GetEnumerator()

But this doesn't compile No implementation was given for 'Collections.IEnumerable.GetEnumerator()

How do i do this?

like image 976
Paul Nikonowicz Avatar asked Apr 10 '12 21:04

Paul Nikonowicz


1 Answers

In F# seq is really just an alias for System.Collections.Generic.IEnumerable<T>. The generic IEnumerable<T> also implements the non-generic IEnumerable and hence your F# type must do so as well.

The easiest way is to just have the non-generic one call into the generic one

type A(collection : seq<string>) =
  member this.Collection with get() = collection

  interface System.Collections.Generic.IEnumerable<string> with
    member this.GetEnumerator() =
      this.Collection.GetEnumerator()

  interface System.Collections.IEnumerable with
    member this.GetEnumerator() =
      upcast this.Collection.GetEnumerator()
like image 181
JaredPar Avatar answered Nov 20 '22 15:11

JaredPar