77909

Initializing an empty file value in Elm

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.

Recommend

  • Can I put a border radius on a before or after pseudo selector image?
  • What is the difference between using img tag src attribute in HTML to display image and styling it i
  • Detecting when a div overflows
  • Duplicate Image in Gallery
  • nested for loop difficulties
  • Play button centred with different image/video sizes
  • How to do backbone image slider
  • javascript: smallest JSON.stringify for Float32Array?
  • How to get Some(x) from the results of Futures#awaitAll?
  • Clear Pole Display text using chrome serial
  • FileReader readAsArrayBuffer in IE11 not working for large files
  • JS - How to compute MD5 on binary data
  • Bootstrap 4 carousel responsive issue in chrome
  • Chromecast sender as a PlayReady license provider
  • using a table in a different class
  • How to call calling a function inside a Template literal
  • FFmpeg NaCl module avformat_open_input (on rtsp stream) returns -5: I/O error
  • PHP base64_decode C# equivalent
  • how to divide a large Elm program into smaller components
  • How to create new layer or new protocol with scapy?
  • align 3 divs inline
  • Special Characters in Google Calculator
  • Are IEnumerable collection supported in EF 4.1 for mapping?
  • Are there any callbacks available in the eventbrite api?
  • Run cakephp app in apache alias
  • Specify source IP address for TCP socket when using Linux network device aliases
  • How to alias an executable using Powershell permanently?
  • Removing html tags using python?
  • Extend Name Box
  • How to remove last utf8 char of a python string
  • Imageloader not loading image on real device
  • Gstreamer1.0 : link a decodebin to videoconvert
  • Rodeo UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal
  • Unable to decode certificate at client new X509Certificate2()
  • xtable package: Skipping some rows in the output
  • Jackson Parser: ignore deserializing for type mismatch
  • Very simple C++ DLL that can be called from .net
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Why is an OPTIONS request sent to the server?