Initializing an empty file value in Elm

柔情痞子 提交于 2019-12-13 21:50:32

问题


I am using Elm (0.18) and imported simonh1000's FileReader library. To store a file value, we use the following type:

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

I want to initialize my model with an empty placeholder. I do this as follows:

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

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

But the compiler gives me this error:

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

    Model

But the definition (shown above) is a:

    { username : String
    , filecontent : Maybe a
    }

回答1:


Since Json.Decode.Value is an alias for Json.Encode.Value, if you really want to initialize a Value type as a JSON {}, you can do the following:

filecontent = Json.Encode.object []

However, I think in your case, it makes more sense to refactor to a Maybe FileContentArrayBuffer field type, since, what would you do with an Value type that decodes to {} anyway? A Nothing value definitely seems more fitting and idiomatic.



来源:https://stackoverflow.com/questions/47541291/initializing-an-empty-file-value-in-elm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!