Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Syntax F中项属性的类型#_Syntax_F#_Interface_Functional Programming - Fatal编程技术网

Syntax F中项属性的类型#

Syntax F中项属性的类型#,syntax,f#,interface,functional-programming,Syntax,F#,Interface,Functional Programming,考虑接口: type IVector = abstract Item : int -> float 现在,让我们定义类: type DenseVector(size : int) = let mutable data = Array.zeroCreate size interface IVector with member this.Item with get n = data.[n] 提供一种方法来改变稠密向量的第n个条目怎么样?然后,

考虑接口:

type IVector = 
    abstract Item : int -> float
现在,让我们定义类:

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

    interface IVector with 
        member this.Item with get n = data.[n]
提供一种方法来改变稠密向量的第n个条目怎么样?然后,最好将上述代码修改为:

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
类型DenseVector(大小:int)=
让可变数据=Array.zeroCreate size
接口IVector与
使用get n=data成员此.Item。[n]

并设置n value=data。[n]您可以在不更改原始接口的情况下实现DenseVector,同时提供如下设置器:

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
类型IVector=
抽象项:int->float with get
类型DenseVector(大小:int)=
让data=Array.zeroCreate size
接口IVector与
使用get i=data来成员此.Item。[i]
成员:本项目
使用get i=(this:>IVector)。[i]

设置i值=数据。[i]非常好。非常感谢你@毛里西奥,是的。也就是说,如果您希望通过接口调用setter,则是这样。如果没有,并且接口中只有getter,那么在这里取消对setter的注释仍然是非法的:“public class V:IV{double IV.this[int x]{get{return 0.0;}/*set{}*/}”(回想一下,F#中的所有接口都是显式的),您必须与接口分开实现该属性。
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