Msg在elm输入中有额外变量

时间:2017-10-24 15:24:52

标签: elm

我正在尝试将Elm(0.18)中的Msg值划分为不同的类型。我希望它符合这种打字:

type MsgSession
    = LogIn
    | LogOut


type MsgSettings
    = SetUsername String
    = SetPassword String

type Msg
    = SessionMsg MsgSession
    | SettingsMsg MsgSettings
    ...

我的更新功能如下所示:

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        SessionMsg sessionMsg ->
            sessionUpdate sessionMsg model

        SettingsMsg settingsMsg ->
            settingsUpdate settingsMsg model
        ...

...这意味着我还必须定义子更新......

sessionUpdate : MsgSession -> Model -> ( Model, Cmd Msg )
sessionUpdate msg model =
    case msg of
        LogIn ->
            -- do stuff
        LogOut ->
            -- do stuff

我的LogOut事件看起来像这样,并且工作正常:

button [onClick (SessionMsg LogOut)] [text "Log Out"]

但是一旦有一个变量涉及该事件,它就无法运作。我对settingsUpdate有类似的设置:

settingsUpdate : MsgSettings -> Model -> ( Model, Cmd Msg )
settingsUpdate msg model =
    case msg of
        SetUsername string ->
           ...
        SetPassword string ->
           ...

但我无法将onInput作为变量发送。例如,此代码

input [onInput SettingsMsg SetUsername] []

产生此错误:

Function `onInput` is expecting 1 argument, but was given 2.
483|                                   onInput SettingsMsg SetUsername

括号也不起作用。

input [onInput (SettingsMsg SetUsername)] []

产量

The argument to function `SettingsMsg` is causing a mismatch.
483|                                            SettingsMsg SetUsername)
                                                            ^^^^^^^^^^^
Function `SettingsMsg` is expecting the argument to be:
    MsgSettings
But it is:
    String -> MsgSettings

我需要传递给onInput才能使其发挥作用?

1 个答案:

答案 0 :(得分:1)

你应该能够使用作文:

input [onInput (SettingsMsg << SetUsername)] []

如果你对显式lambdas感觉更舒服,那么看起来像这样:

input [onInput (\name -> SettingsMsg (SetUsername name))] []