
Question:
I am using Elm (0.18) and imported simonh1000's <a href="https://github.com/simonh1000/file-reader" rel="nofollow">FileReader</a> 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
}
Answer1:Since <a href="http://package.elm-lang.org/packages/elm-lang/core/5.1.1/Json-Decode#Value" rel="nofollow">Json.Decode.Value
</a> is an alias for <a href="http://package.elm-lang.org/packages/elm-lang/core/5.1.1/Json-Encode#Value" rel="nofollow">Json.Encode.Value
</a>, 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.