Item Property Type in F #

Consider the interface:

type IVector = 
    abstract Item : int -> float

Now define the class:

type DenseVector(size : int) = 
    let mutable data = Array.zeroCreate size

    interface IVector with 
        member this.Item with get n = data.[n]

How to provide a method for changing the nth record of a dense vector? Then it would be nice to change the above code as:

type DenseVector(size : int) = 
    let mutable data = Array.zeroCreate size

    interface IVector with 
        member this.Item with get n = data.[n]
                          and set n value = data.[n] <- value

However, I get the following error due to the signature of the abstract method Itemin the interface IVector:

No abstract property was found that matches this override.

So what should be the signature Itemin IVector?

+5
source share
2 answers
type IVector =  
    abstract Item : int -> float with get, set
+8
source

You can implement DenseVector without changing the source interface, also providing a setter as follows:

type IVector = 
    abstract Item: int -> float with get

type DenseVector(size : int) = 
    let data = Array.zeroCreate size
    interface IVector with 
        member this.Item with get i = data.[i]
    member this.Item 
        with get i = (this :> IVector).[i]
        and set i value = data.[i] <- value
+3
source

All Articles