Introduction to Elm, v2 Pure Functions
Transcript from the "Pure Functions" Lesson
>> Richard Feldman: Now, another function guarantee that we have in Elm besides same arguments, same results, is no side effects. Which is to say, whenever you run an Elm function, it's not allowed to modify any external state. It can't, all it can do is return a value, that's it. And that's all it can do.
[00:00:36] But I'm still gonna fire it off. I'm gonna send it off to the server and that's gonna modify the external state that's on the server, like the database. Something's gonna change, but hey, the function, same argument, same result. It just sort of sneakily did this other thing at the same time.
[00:00:50] So in Elm, that's not allowed. You cannot do side effects inside Elm functions. So even though this satisfies the first function guarantee of same arguments, same result, it does not satisfy the second one. And all Elm functions have to satisfy both. Okay, so instead of doing side effects, Elm does what's known as managed effects.
[00:01:11] Which is to say that any side effects have to be done through commands. All you can do in any Elm function that you write, is you can say, yeah, I want to give you back a description of commands that I want to run, or side effects that I want to have happen.
[00:01:24] But the run time is going to perform them. Which means that any Elm function you call, is going to be a pure function, guaranteed. The entire language, everything we've been doing the entire day has been all pure functions. And that's going to continue throughout the rest of the workshop and the entire advanced workshop, because that's the only type of functions that Elm supports.
[00:01:43] All of these side affecting stuff, all the stuff where you get different values depending on context all of that is isolated to the runtime and completely separate from stuff we do as Elm programmers.