Trouble with mapping Tasks

97 views
Skip to first unread message

Daniel Bachler

unread,
Dec 3, 2015, 9:57:46 PM12/3/15
to Elm Discuss
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.readAsDataUrl


I 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 Value
this 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 Action

But 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

Janis Voigtländer

unread,
Dec 3, 2015, 10:09:13 PM12/3/15
to elm-d...@googlegroups.com
Have you considered using toResult?
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Daniel Bachler

unread,
Dec 3, 2015, 10:14:15 PM12/3/15
to elm-d...@googlegroups.com
... Doh! You are right of course. For some reason I thought that this would not match the types I need but it is precisely what I need of course. Thanks for the quick solution!

--
You received this message because you are subscribed to a topic in the Google Groups "Elm Discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elm-discuss/uHXkwJ8MJnw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elm-discuss...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages