如何合并嵌套标记类型的信号?

时间:2015-12-08 21:13:59

标签: elm

作为我正在构建的Elm应用程序的一部分,我希望保持数据更改(将可过滤的模型列表呈现给浏览器)保持环境变化的信号(如调整窗口大小)。我以为我会将它们建模为不同的可扩展类型:

type WindowUpdate = Resize (Int, Int)

type DataUpdate = TagFilter Model.Tag

type Update update data = WindowUpdate update data
                        | DataUpdate update data
                        | NoOp

updates : Signal.Mailbox (Update update data)
updates = Signal.mailbox NoOp

appModel : Signal Model
appModel =
  let
    applicationUpdates = Signal.mergeMany
                       [ updates.signal

                       ]
  in
    Signal.foldp update Model.defaultModel applicationUpdates

windowUpdate : WindowUpdate -> Model -> Model
windowUpdate update model =
    let resizeWidth = \windowModel newWidth -> { windowModel | width = newWidth }
    in
      case update of
        Resize (w, _) -> { model | window = (resizeWidth model.window w) }

update : Update -> Model -> Model
update u model =
  case u of
    WindowUpdate wu data -> windowUpdate (wu data)  model
    DataUpdate du data  -> model
    otherwise       -> model

不幸的是我无法让我的更新功能正常工作。我得到以下编译器错误:

— TYPE MISMATCH —————————————————————— ./app/Updates.elm

The 3rd argument to function `foldp` is causing a mismatch.

36│     Signal.foldp update Model.defaultModel applicationUpdates
                                               ^^^^^^^^^^^^^^^^^^
Function `foldp` is expecting the 3rd argument to be:

    Signal (Update a)

But it is:

    Signal Update

Hint: I always figure out the type of arguments from left to right. If an
argument is acceptable when I check it, I assume it is "correct" in subsequent checks. So the problem may actually be in how previous arguments interact with the 3rd.

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您忘记了Update签名中update的类型参数,它应该是(代码未经测试):

update : Update update data -> Model -> Model