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?
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()
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