Beginner: Help with tasks: now : Task x Time

170 views
Skip to first unread message

Niklas Koponen

unread,
Jun 22, 2016, 7:53:20 AM6/22/16
to Elm Discuss
Hi!

I'm trying to get the current time in elm but I can't figure out how use tasks. Now definition looks like this

now : Task x Time

The guide regarding tasks says:

Second, notice that the error type is the type variable x. When we saw Http.getString it had a concrete error type Http.Error. The Time.now task is different in that it will not fail in a special way. You will always get the time back. This type variable x is basically saying “I am not going to force you to handle a particular type of errors” because it does not produce any.

I have this message:

NewEntry Time

The problem is I don't know what to write for the error when I do Task.perform

Task.perform NewEntry [WHAT HERE?] Time.now

-Niklas

Janis Voigtländer

unread,
Jun 22, 2016, 8:00:21 AM6/22/16
to elm-d...@googlegroups.com
Since Time.now cannot fail (visible as per the type variable x), you can simply use http://package.elm-lang.org/packages/NoRedInk/elm-task-extra/2.0.0/Task-Extra#performFailproof, which relieves you from writing anything for the supposed fail case.
--
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.

Niklas Koponen

unread,
Jun 22, 2016, 8:28:43 AM6/22/16
to Elm Discuss
Thank you! That did the trick.

Took a look at the Task.Extra source and I have to admit it would have been impossible for me to come up with that solution.

-Niklas

Janis Voigtländer

unread,
Jun 22, 2016, 8:34:02 AM6/22/16
to elm-d...@googlegroups.com
That's why it is a provided function. :-)

--

Ian Mackenzie

unread,
Jun 23, 2016, 8:01:52 PM6/23/16
to Elm Discuss
An alternate approach is just to use 'never' from elm-community/basics-extra directly for your '[WHAT HERE?]'. Although the error case actually comes first, so your example should be

Task.perform [WHAT HERE?] NewEntry Time.now

and my preferred answer would be (assuming 'import Basics.Extra exposing (never)')

Task.perform never NewEntry Time.now

It ends up being exactly the same thing under the hood - I just prefer this version because I find it very explicit, it's a little bit shorter, and most importantly it builds a nice solution out of little generic, modular pieces instead of using a more specialized function (if you can get used to how 'never' is used here, then you might be able to find uses for it elsewhere).
Reply all
Reply to author
Forward
0 new messages