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
Struct F#可变值是不可变的_Struct_F#_Mutable - Fatal编程技术网

Struct F#可变值是不可变的

Struct F#可变值是不可变的,struct,f#,mutable,Struct,F#,Mutable,我有下面的结构,它应该允许我创建,然后修改时间和值属性,这将更改私有可变值x.time和x.value type TimeDataPoint = struct val mutable private Time_: DateTime val mutable private Value_: double new (time: DateTime, value: double) = {

我有下面的结构,它应该允许我创建,然后修改时间和值属性,这将更改私有可变值x.time和x.value

type TimeDataPoint = 
    struct
        val mutable private Time_: DateTime
        val mutable private Value_: double

        new (time: DateTime, value: double) = 
            {
                Time_ = time
                Value_ = value
            }

        member public x.Time
            with get() = x.Time_
            and set(time: DateTime) = x.Time_ <- time

        member public x.Value
            with get() = x.Value_
            and set(value: double) = x.Value_ <- value

    end

我该如何解决这个问题?

您还需要将您的
tdp
标记为可变。精简代码如下所示,并输出:

Ticks is 0
Ticks is 636028131920527873
正如所料

module Mutation
open System
type TimeDataPoint = 
    struct
        val mutable private Time_: DateTime
        new (time: DateTime) = 
            {
                Time_ = time
            }
        member public x.Time
            with get() = x.Time_
            and set(time: DateTime) = x.Time_ <- time
    end

let usingTdp() = 
    let mutable tdp = TimeDataPoint()
    printfn "Ticks is %i" tdp.Time.Ticks
    tdp.Time <- DateTime.Now
    printfn "Ticks is %i" tdp.Time.Ticks

如果你和F一起工作,你可能会考虑重新思考变异是否真的是你唯一的选择——我怀疑是。

PS:如果您决定使用一个突变对象,请考虑使用Auto属性语法,它允许您将类定义缩短为:

type TimeDataPoint(v, d) = 
    member val Value: int = v with get, set
    member val Time: DateTime = d with get, set

只要我将
tdp
定义为
let mutable tdp=TimeDataPoint()
,就可以很好地工作。你能发布更多的内容吗?你最好的选择是不要走这条路。NET struct设计指南建议您最好避免可变值类型:您要求通过使可变结构变得有意义而进入一个混乱的世界。我将结构更改为常规类。谢谢。您能发布问题的完整副本吗?
TimeDataPoint
中存储的变量只有在
TimeDataPoint
为结构时才需要可变。如果它是一个类,那么该变量将保存对该类实例的引用,并且在修改该类的属性时,该引用不需要更改。对于没有引用的结构,数据存储在变量中,因此修改结构意味着修改变量,这就是为什么变量本身需要是可变的。
module Mutation
open System
type TimeDataPoint = 
    struct
        val mutable private Time_: DateTime
        new (time: DateTime) = 
            {
                Time_ = time
            }
        member public x.Time
            with get() = x.Time_
            and set(time: DateTime) = x.Time_ <- time
    end

let usingTdp() = 
    let mutable tdp = TimeDataPoint()
    printfn "Ticks is %i" tdp.Time.Ticks
    tdp.Time <- DateTime.Now
    printfn "Ticks is %i" tdp.Time.Ticks
type TimeDataPoint = 
    val mutable private _v: int
    new (v: int) = 
        {
            _v = v
        }
    member public this.Value
        with get() = this._v
        and set(v: int) = this._v <- v

let myList = LinkedList<TimeDataPoint>()
myList.AddFirst(TimeDataPoint(1)) |> ignore
myList.First.Value.Value <- 1
type TimeDataPoint(v, d) = 
    member val Value: int = v with get, set
    member val Time: DateTime = d with get, set