Introduction to Elm, v2

Primitives & Records

Richard Feldman

Richard Feldman

Vendr, Inc.
Introduction to Elm, v2

Check out a free preview of the full Introduction to Elm, v2 course

The "Primitives & Records" Lesson is part of the full, Introduction to Elm, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Examples of how to use type annotations on primitives when the type changes is demonstrated. Record type annotations are also explained, and it becomes clear why the syntax for records is different than in JavaScript. - couldn't find the cut here in real-time


Transcript from the "Primitives & Records" Lesson

>> Richard Feldman: Another way I could write an annotation is for something like this. I could say, totalPages is an integer, that's true. So Elm has integers as well as floats. So floating points, that would be like a decimal. Also, Boolean. So isActive, Bool, that works. Here's how we can annotate functions.

So I could say the function round, goes from the float to an int by rounding it off, there is also floor and ceiling, which have the same type annotation. There is the not function, which we saw earlier, that takes true and turns it into false or vice versa, that's bool to bool.

The arrow basically says, here is what I'm taking in and then here's what I'm returning. We have a String.length, which gives you back the length of a string, that takes a string and returns on int. We can also do record types in our annotations. So this kind of demonstrates that's why elm has this different syntax for colon and equals.

If you see these kind of stacked up like this. Colon is used for types in Elm, whereas equals is used for values. So that's why there's a slightly different syntactic difference between Elm and JavaScript in terms of how these literals look. So in JavaScript, there is no type annotation that uses colons, so colon can mean whatever.

Whereas the Elm, it wants to be consistent. As you can see on the bottom here, where we're using equals for the values and then colon for the types corresponding to those values.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now