【问题标题】:Adding to map as attribute in a record f#添加到地图作为记录 f# 中的属性
【发布时间】:2019-03-25 21:51:51
【问题描述】:

我有这种类型:

type Model = {Ships: map<Mmsi, Ship>}
type Ship = {Latitude: float Longitude: float Speed: float Heading: float}

我正在想办法通过消息类型添加到船舶地图中

type msg = {Latitude: float Longitude: float Mmsi = int Time = string}

使用此功能:

let update (msg : Msg) (currentModel : Model) : Model =
    // TODO: implement the new model based on the received message here
    let currentModel = 
        { 
            Ships = Map.Add(msg.Mmsi, msgToShip msg)
        }
    currentModel

但是我从编译器中收到这样的错误:“方法或对象构造函数 'Add' 不是静态的。

我在 f# 或函数式编程方面并不是特别有经验,所以任何指向这方面的指针都将不胜感激。

【问题讨论】:

    标签: dictionary types f# functional-programming


    【解决方案1】:

    大写的AddMap&lt;_, _&gt; 类型的实例方法。小写的addMap 模块中的一个函数。所以你想在这里调用的是:

    Ships = Map.add msg.Mmsi (msgToShip msg) currentModel.Ships
    

    或者,也有效但不太惯用:

    Ships = currentModel.Ships.Add(msg.Mmsi, msgToShip msg)
    

    【讨论】:

    • 感谢您的回答!
    猜你喜欢
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 2017-08-04
    相关资源
    最近更新 更多