简体   繁体   English

F#中Item属性的类型

[英]Type of the Item property in F#

Consider the interface: 考虑界面:

type IVector = 
    abstract Item : int -> float

Now, let us define the class: 现在,让我们定义类:

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

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

What about supply a method to mutate the n-th entry of the dense vector? 如何提供一种方法来改变密集向量的第n个条目? Then, it would be nice to modify 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 because of the signature of the abstract method Item in the IVector interface: 但是,由于IVector界面中抽象方法Item的签名,我收到以下错误:

No abstract property was found that corresponds to this override. 未找到与此覆盖对应的抽象属性。

So, what should be the signature of Item in IVector ? 那么,在IVectorItem的签名应该是什么?

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

You can implement DenseVector without changing the original interface while also providing a setter like this: 您可以在不更改原始界面的情况下实现DenseVector,同时还提供如下的setter:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM