尤川豪   ·  3年前
445 貼文  ·  275 留言

Elm type 學習筆記

type Msg
    = SendHttpRequest
    | DataReceived (Result Http.Error (List String))
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        SendHttpRequest ->
            ( model, getNicknames )

        DataReceived (Ok nicknames) ->
            ( { model | nicknames = nicknames }, Cmd.none )

        DataReceived (Err httpError) ->
            ( { model
                | errorMessage = Just (buildErrorMessage httpError)
              }
            , Cmd.none
            )

這段看了好幾天看不太懂

終於懂了

Msg 可以稱之為 union type

SendHttpRequest 不只是一個 value 更是一個只含有一個 value 的集合

DataReceived 是 type constructor

(Result Http.Error (List String)) 是 type variable

這個 type variable 真正的 type 又是由 Result 這個 type constructor 吃進兩個 type variable 產生

所以 case msg of 列舉出來的 是 Msg 這個 union type 裡面各個集合的聯集

所以 SendHttpRequest 是一種 value

DataReceived (Ok nicknames) 是一種 value

DataReceived (Err httpError) 是一種 value

不知道以上這樣描述正確嗎?

目前理解到這邊,持續努力中

  分享   共 774 次點閱
共有 0 則留言
還沒有人留言。歡迎分享您的觀點、或是疑問。
您的留言
尤川豪
445 貼文  ·  275 留言

Devs.tw 是讓工程師寫筆記、網誌的平台。隨手紀錄、寫作,方便日後搜尋!

歡迎您一起加入寫作與分享的行列!

查看所有文章