问题
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