[英]How do i get the a value from Decoder type in Elm
我目前正在嘗試更新其他人從 0.18 開始制作的舊項目。
我有 Msg 和 Mouse 類型
type Msg =
SelectElement Int
| ApplyTool (Maybe Int) Mouse
type alias Mouse =
{
x: Int
, y: Int
}
問題是我正在使用 Json.Decode 進行一些映射,因此最終得到類型為: Decoder Msg
的值
有沒有一種簡單的方法可以從 Msg 中獲取值?
Decoder
不保存值,它知道如何從 JSON 輸入生成值(如果 JSON 格式不正確,則失敗)。
如果您嘗試調整正在解碼的值,您可以使用Json.Decode.map
或Json.Decode.andThen
。 也許它看起來像這樣:
mouseDecoder =
Json.Decode.map2 (\x y -> Mouse x y)
(Json.Decode.field "pageX" Json.Decode.int)
(Json.Decode.field "pageY" Json.Decode.int)
msgDecoder =
mouseDecoder
|> Json.Decode.map (\mouse -> ApplyTool Nothing mouse)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.