Chapter 3. Why Types?
Now that you know a bit about reading Elm, let’s learn more about types. If you’re coming from a background in JavaScript or Python, talking directly about types might feel a little foreign, but types in Elm provide developers with specific benefits:
-
No runtime exceptions in practice.
-
Specific, developer-friendly error messages at compile time.
-
Easy refactoring. Elm’s types ensure that you won’t break anything when you need to make a big change. This means it’s easy to keep codebases cruft-free and well designed.
-
Enforced semantic versioning of Elm packages. There should be no reason that a patch change can break an API. Elm automatically enforces the version number of a package, so you know this is always true.
-
Extremely reusable code. One of the ultimate goals of software is code reuse, but too often it’s something that’s hard to achieve. Because of Elm’s types, Elm functions are inherently easy to reuse, much more so than functions in JavaScript, Python, or TypeScript. This, and the fact that Elm’s functions are so easy to test, is directly due to Elm’s use of immutable data, which we’ll cover in a little bit.
-
Language-wide optimization of external effects. You want to send an HTTP request? You’re rendering HTML into the DOM? In either situation, Elm will make sure all external actions are done efficiently, with minimal effort needed from a developer to optimize them.
We’ll cover these benefits in more detail in the following sections.
Get Why Elm? now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.