Custom IEnumerator in F #

Additional questions F #. The following is a binary read implementation. I want it to work as an enumerated sequence. The code below gives me the following error, and I, as usual, do not know how to solve it. I have a C # implementation where I had to implement two different overrides for a property .Current. I think I should do the same here, but I don’t know how to do it. As always, thanks for the million for your help.

error FS0366: for Collections.IEnumerator.get_Current() : objnot implemented implementation. Note that all interface members must be implemented and listed in the corresponding declaration interface, for example. interface ... with member ....

namespace persisitence
open System.Collections.Generic
open System
open System.IO
type BinaryPersistenceIn<'T>(fn: string, serializer: ('T * BinaryReader) -> unit) as this =
    let stream_ = File.Open(fn, FileMode.Open, FileAccess.Read)
    let reader_ = new BinaryReader(stream_)
    [<DefaultValue>] val mutable current_ : 'T

    let eof() =
         stream_.Position = stream_.Length


    interface IEnumerator<'T> with

        member this.MoveNext() = 
            let mutable ret = eof()

            if stream_.CanRead && ret then
                serializer(this.current_, reader_)

            ret

        member this.Current
            with get() = this.current_ 

        member this.Dispose() =
            stream_.Close()
            reader_.Close()

        member this.Reset() = 
            stream_.Seek((int64) 0., SeekOrigin.Begin) |> ignore
+4
source share
3 answers

@Richard, IEnumerator.Current.
" ". :

: ( @DaxFohl)

  • IEnumerator (. ).
  • MoveNext Reset IEnumerator, IEnumerator<'t>, , .
  • Dispose, , IEnumerator<'t> (surprise!: -)

-

type BinaryPersistenceIn<'T>(fn: string, serializer: ('T * BinaryReader) -> unit) as this =
    ...    

    interface IEnumerator<'T> with
        ...
        member this.Current
            with get() = this.current_ 

    interface System.Collections.IEnumerator with
        member this.Current
            with get() = this.current_ :> obj
        member this.MoveNext() = ...
        member this.Reset() = ...

: IEnumerator? , . ?

let binaryPersistenceSeq (fn: string) (serializer: BinaryReader -> 'T) = 
  seq {
    use stream_ = File.Open(fn, FileMode.Open, FileAccess.Read)
    use reader_ = new BinaryReader(stream_)

    let eof() = stream_.Position = stream_.Length

    while not eof() do
       if stream_.CanRead then
          yield serializer reader_
  }
+4

IEnumerator<T> extends IEnumerator IEnumerator Current object.

IEnumerator.Current IEnumerator<T>.Current.

+1

... .. .

type BinaryPersistenceIn<'T>(fn: string, serializer: ('T * BinaryReader) -> unit) =
let stream_ = File.Open(fn, FileMode.Open, FileAccess.Read)
let reader_ = new BinaryReader(stream_)
[<DefaultValue>] val mutable current_ : 'T

let eof() =
     stream_.Position = stream_.Length


interface IEnumerator<'T> with

    member this.MoveNext() = 
        let mutable ret = eof()

        if stream_.CanRead && ret then
            serializer(this.current_, reader_)

        ret

    member this.Current
        with get() = this.current_ 

    member this.Dispose() =
        stream_.Close()
        reader_.Close()

    member this.Reset() = 
        stream_.Seek((int64) 0., SeekOrigin.Begin) |> ignore

    member this.Current
        with get() = this.current_ :> obj
0

All Articles