re: What is functional? VIEW POST


This value has the Cmd type (actually Cmd msg, but forget about the msg part for now). It's a 'command'. By itself, reload doesn't do much, it's just a value. But if the Elm runtime evaluates the update function, and the result is a reload command, the runtime will reload the page.

Because all effects are the return values of functions, there are no side-effects that happened but were not returned. This ensures that, in declarative languages, we can safely compose effects.

This feels peculiar to me, but probably because I'm not familiar with Elm or an expert in FP. By having the 'effect' caused by the runtime reading a value? Or the effect is recorded by the value? When does the page reload actually happen, and how is it not a side effect?


Good question.

The update function in Elm takes a message and a model (the state of the website) as input. It produces a new, updated model, and a command (Cmd). At this point, the command is just data, same as a string or int.

The Elm runtime calls the update function whenever something relevant happens. The runtime then starts using the new model and executes the command, turning data into action.

and how is it not a side effect?

The complete answer to this will take a few more posts. In short: it's completely described in the type system.

In this specific case, it isn't even really an effect, rather a description of an effect.

It isn't wrong to consider a Cmd to be a side-effect when it is being evaluated, but in Elm we're never writing code at that level. We only consider the world as it is at one particular point in time. Haskell does let you do this (excuse the pun), it starts imperative and lets you evaluate pure functions from there. I expect future languages can drop impurities altogether thanks to advances in type systems, but I'm getting ahead of myself here :P

code of conduct - report abuse