Elm'Json.Decode.succeed':如果应该始终返回相同的值,如何在解码管道中使用它?

问题描述 投票:1回答:1

我正在学习榆木,让我感到困惑的一件事是'Json.Decode.succeed'。根据docs

succeed : a -> Decoder a

Ignore the JSON and produce a certain Elm value.

decodeString (succeed 42) "true"    == Ok 42
decodeString (succeed 42) "[1,2,3]" == Ok 42
decodeString (succeed 42) "hello"   == Err ...

我了解(尽管作为一个初学者,我还没有看到它的用途)。但是在解码管道中也使用了此方法,因此:]somethingDecoder : Maybe Wookie -> Decoder Something somethingDecoder maybeWookie = Json.Decode.succeed Something |> required "caterpillar" Caterpillar.decoder |> required "author" (Author.decoder maybeWookie)

这里发生了什么?也就是说,如果“成功”忽略了传递给它的JSON,它如何用于读取JSON并将其转换为Elm值?任何线索表示赞赏!

我正在学习榆木,让我感到困惑的一件事是'Json.Decode.succeed'。根据文档成功:a->解码器a忽略JSON并产生一定的Elm值。 encodeString(...

json elm
1个回答
0
投票
首先,对解码器管道的直觉是它的作用就像一个咖喱函数,其中带有requiredoptional的管道一一应用自变量。期望所有功能,包括函数及其参数

返回值都包装在Decoder中。
© www.soinside.com 2019 - 2024. All rights reserved.