--
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.
Maybe that task can fail if you run the Elm program with .worker rather than .embed or .fullscreen.
No, that task definitely cannot fail. Otherwise it would not be able to have the following type in the Window module:
size : Task x Size
You should be able to define performSucceed yourself (and with a built-in guarantee that the failing case will definitely not occur):
performSucceed : (a -> msg) -> Task Never a -> Cmd msg
performSucceed = Task.perform never
never : Never -> a
never n = never n
which (in particular never) may look daunting, but isn’t. That never function could be hidden away from you, see https://github.com/elm-lang/core/pull/593.
Why does it use infinite recursion and not this:
About this:
However I don’t understand how it works -.-> That never function looks crazy indeed. Why does it use infinite recursion and not this:
never n = Debug.crash “Well, we’re doomed _(ツ)_/“
Joey has provided the answer.
About this:
Also regarding my second question, why does Window.size not return a Cmd?
What benefits does a Task that cannot fail have over a Cmd?
Tasks are more composable, which you might need in some situations (combining the Window.size with some other task before turning the result into a Cmd). The key here is that nothing like http://package.elm-lang.org/packages/elm-lang/core/4.0.0/Task#andThen exists for Cmd.
Let’s say you want to make an http request where the url to request should depend on the current time. You can build a task that does this by combining Time.now and Http.getString. Something like Time.now `andThen` \time -> Http.getString (queryStringBuildFrom time). Then you turn that combined task into a Cmd and return it from your update function. If you were to try to do the same with just Cmds, you would have to do Time.now, then have an extra round of the update function in which you receive the resulting time, turn it into an Http request Cmd, send that off, and wait for its result. That’s not necessarily what you want, the extra update round, and no guarantee that no other message occurs in between. (That is, with the Cmd-only thing, you don’t even know that the two involved update rounds happen directly after each other. With Task, you have built a thing consisting of time getting and request sending that will definitely happen directly one after the other. At least with the current state of the Task and scheduler implementation.)
Actually, maybe a more useful description would frame this not as what Cmd does not have that Task has, namely andThen, but instead frame it as what Cmd offers in addition. It’s the “managed effects” aspect discussed at http://guide.elm-lang.org/effect_managers/. The two examples you mentioned, Time.now and Random.generate, are nor actually as similar as you seem to assume. To support Time.now, the implementation doesn’t need any effect management in the background. It just asks the operating system for the current time, and done. To support Random.generate, the implementation does need to do effect management (magic) in the background. Namely, it needs to keep track of a hidden seed for the random generator. That must be preserved/propagated over several calls throughout the program’s life. That would not be possible with a Task, but it is possible with a Cmd, thanks to the concept of effect managers. That’s also the reason why a Task can be turned into a Cmd, but a Cmd can’t be turned into a Task. So when looking through the available core and platform libraries, and wondering about why some things are Tasks and others are Cmds, the reasoning in my understanding is that something is a Task if it can be, if it can be implemented without effect management magic behind the scenes, whereas something is a Cmd if it has to be, if it could not be implemented without hidden effect management.