Hi,
I am new to Elm and I am having trouble mapping a Task. The code I am working on is based on
https://github.com/simonh1000/file-reader which in turn is pretty close to the example 5 in the elm architecture
https://github.com/evancz/elm-architecture-tutorial/blob/master/examples/5/RandomGif.elm .
The problem I have is in the method that get's called as part of the update function when the user gives me a file to load. There is a native method that expects a JS File object and returns a Task giving the content as a DataURL (or an error).
It looks like this:
readAsDataUrl : Json.Value -> Task Error Json.Value
readAsDataUrl = Native.FileReader.readAsDataUrlI try to use it like so, but this doesn't work
loadData : Json.Value -> Effects Action
loadData file =
readAsDataUrl file
|> Task.map wrapInResult
`Task.onError` (\err -> Task.succeed (Result.Err err))
|> Task.map LoadImageCompleted
|> Effects.task
wrapInResult : Json.Value -> Result a Json.Value
wrapInResult val = Result.Ok val What I expected this to do was
readAsDataUrl returns a
Task Error Valuethis is mapped to a
Task Error (Result a Value)the error case is mapped to a
Task x (Result Error Value)the task is mapped into a
Task x Action // one of the action cases expects a result as specified in the line above)
the task is turned into an
Effects ActionBut what happens is that I get two type mismatch errors:
λ elm make .\DataUrlExample.elm
-- TYPE MISMATCH ------------------------------------------ .\DataUrlExample.elm
The right argument of (|>) is causing a type mismatch.
122│ readAsDataUrl file
123│> |> Task.map wrapInResult
124│> `Task.onError` (\err -> Task.succeed (Result.Err err))
(|>) is expecting the right argument to be a:
Task.Task Error Value -> a
But the right argument is:
Task.Task a (Result b c)
Hint: I always figure out the type of the left argument first and if it is
acceptable on its own, I assume it is "correct" in subsequent checks. So the
problem may actually be in how the left and right arguments interact.
-- TYPE MISMATCH ------------------------------------------ .\DataUrlExample.elm
The left argument of `onError` is causing a type mismatch.
123│> Task.map wrapInResult
124│ `Task.onError` (\err -> Task.succeed (Result.Err err))
`onError` is expecting the left argument to be a:
Task.Task a b
But the left argument is:
Task.Task a Value -> Task.Task a (Result Error Value)
Detected errors in 1 module.
I thought that the type signatures should check out, but I am obviously missing something. Unfortunately, I don't really understand where the compiler and I differ. Does it have to do with the concrete bound types for Result?
I had the whole thing working when I used toMaybe instead of mapping and `onError` and gave the action a Maybe Value, but I would like to keep the error so I can display a decent error message, hence the attempt to use the Result type.
Any help would be very much appreciated. Thanks!
Daniel