问题描述
我正在使用Elm(0.18),并导入了simonh1000的库。要存储文件值,我们使用以下类型:
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
}
推荐答案
由于是 Json.Encode.Value
,如果您真的要初始化ea Value
类型作为JSON {}
,您可以执行以下操作:
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 []
但是,我认为在您的情况下,重构为也许是FileContentArrayBuffer
字段类型更有意义,因为,您如何处理 Value
类型会解码为 {}
吗? 没什么
的值显然更合适和习惯。
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.
这篇关于在Elm中初始化一个空文件值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!