簡體   English   中英

在記錄f#中作為屬性添加到地圖

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

我有這種類型:

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

但是我從這樣的編譯器中得到一個錯誤:“方法或對象構造函數“添加”不是靜態的。

一般而言,我在f#或函數式編程方面沒有特別的經驗,因此,對此的任何提示將不勝感激。

大寫字母Add是類型Map<_, _>的實例方法。 小寫add是“ Map模塊中的函數。 因此,您想在這里打電話的是:

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

或者,也是有效的,但不太習慣:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM