在 Elm 中初始化一个空文件值

Initializing an empty file value in Elm

我正在使用 Elm (0.18) 并导入了 simonh1000 的 FileReader 库。要存储文件值,我们使用以下类型:

import Json.Decode as Json exposing (Decoder, Value)
...
{-| An ArrayBuffer is a Elm Json Value.
-}
type alias FileContentArrayBuffer =
    Value

我想用一个空占位符初始化我的模型。我这样做如下:

type alias Model = 
  {
     username : String
   , filecontent: FileContentArrayBuffer
  }

initialModel : Model
initialModel = 
  {
     username = "mark"
   , filecontent = Nothing
  }

但是编译器给我这个错误:

The type annotation for `initialModel` says it is a:

    Model

But the definition (shown above) is a:

    { username : String
    , filecontent : Maybe a
    }

Json.Decode.Value is an alias for Json.Encode.Value开始,如果你真的想把一个Value类型初始化为一个JSON{},你可以这样做:

filecontent = Json.Encode.object []

但是,我认为在您的情况下,重构为 Maybe FileContentArrayBuffer 字段类型更有意义,因为,您将如何处理解码为 [=14= 的 Value 类型] 反正? Nothing 值显然更合适和惯用。