简体   繁体   English

在记录f#中作为属性添加到地图

[英]Adding to map as attribute in a record f#

I have this type: 我有这种类型:

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

I am trying to figure out a way to add to the ship map through a message type 我正在尝试找出一种通过消息类型添加到飞船地图的方法

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

Using this function: 使用此功能:

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

However I am getting an error from the compiler as such: "Method or object constructor 'Add' is not static. 但是我从这样的编译器中得到一个错误:“方法或对象构造函数“添加”不是静态的。

I am not especially experienced in f# or functional programming in general so any pointers to this would be most appreciated. 一般而言,我在f#或函数式编程方面没有特别的经验,因此,对此的任何提示将不胜感激。

Capitalized Add is an instance method on the type Map<_, _> . 大写字母Add是类型Map<_, _>的实例方法。 Lowercase add is a function in the Map module. 小写add是“ Map模块中的函数。 So what you wanted to call here was: 因此,您想在这里打电话的是:

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

Or, also valid but less idiomatic: 或者,也是有效的,但不太习惯:

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

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

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