将 Msg case 值作为泛型传递给另一个执行 Http.Post 的函数?

Passing a Msg case value as a generic to another function that performs a Http.Post?

我正在尝试执行以下操作:

   Submit ->
        ( model, runtime.tryRegister model Response )

其中 Response 定义为:

| Response (Result Http.Error JsonProfile)

但是,以下函数无法编译:

tryRegister : Form -> (Result Http.Error a -> msg) -> Cmd msg
tryRegister form msg =
    let
        registerUrl =
            "http://localhost:5000/register"

        body =
            encode form |> Http.jsonBody

        request =
            Http.post registerUrl body decoder
    in
        Http.send msg request

错误:

Http.send msg request

The 2nd argument to function send is causing a mismatch. - Function send is expecting the 2nd argument to be:

Http.Request a

But it is:

Http.Request JsonProfile

这是解码器:

decoder : Decoder JsonProfile
decoder =
    Decode.map4 JsonProfile
        (field "Id" Decode.int)
        (field "FirstName" Decode.string)
        (field "LastName" Decode.string)
        (field "Email" Decode.string)

source code can be found on GitHub.

如果您不想让 tryRegister 函数与 JsonProfile 绑定,那么您需要将解码器作为参数传入,而不是引用顶级函数 decoder,它被键入 JsonProfile.

tryRegister : Form -> Decoder a -> (Result Http.Error a -> msg) -> Cmd msg
tryRegister form decoder msg =
    let
        registerUrl =
            "http://localhost:5000/register"

        body =
            encode form |> Http.jsonBody

        request =
            Http.post registerUrl body decoder
    in
        Http.send msg request

然后您可以在此处传递顶级 decoder 函数:

Submit ->
    ( model, runtime.tryRegister model decoder Response )