Check out a free preview of the full Introduction to Elm, v2 course
The "Decoding" 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:
Richard explains how to decode data from one format to another in Elm, using String.toInt and Json.Decode.decodeString as examples.
Transcript from the "Decoding" Lesson
[00:00:00]
>> Richard Feldman: So we're gonna talk about a few things here. Decoding, result, pipeline decoding and optional and nullable. So each of these is going to build on the stuff we learned in the previous section. Let's start with decoding. So in JavaScript there's a function called parseInt. I can say parseInt "42", that's a string, and JavaScript's gonna turn that into an integer, 42.
[00:00:23]
That is, if it can. Not all strings happen to be integers. So, for example, if I parseInt "hi", JavaScript gives us back Not-a-Number. Now, if you've ever dealt with Not-a-Number, and dealing with the bugs that can result from Not-a-Number, it's really not a good time. This thing propagates throughout all of your operations.
[00:00:42]
Anytime you do arithmetic on Not-a-Number it gives you back another Not-a-Number. So ultimately, it can sort of snake its way through your program until figuring out the root cause, the ultimate culprit of why this thing's Not-a-Number can be a big pain. So Elm doesn't do it like this.
[00:00:56]
Elm does not say, I'm gonna parseInt a string and then it'll give me back an integer, possibly, which is not a number. Elm does it like this, case String.toInt str of, and then it gives us back a maybe, like we saw in the last section. So it'll say either, I got back just a number, in which case it was successful, it was able to parse that string as an integer, it found an integer in there.
[00:01:20]
And it's going to give it to me. Or it's gonna say nothing and say, you know what, I couldn't actually find an integer in that string, so handle that some other way. Okay, String.toInt "42" is the exact syntax we would use. It's not called parsint. It's called String.toInt "42".
[00:01:38]
That will give us back just 42. Strong.toInt "hi" will give us back nothing.
>> Richard Feldman: So the same way that we saw earlier, maybe has this type variable, indicating that it can be used with a variety of different things. In the previous section, we saw it being used for string with the avatar URL example.
[00:01:58]
Here we see it being used with numbers. There's another possibility here, which is [COUGH] using JSON decoders to decode this. This will essentially do the same thing. So JSON decoders are a more general purpose, more flexible version of String.toInt. It's basically saying, I've got some string, and inside that string, I expect to see some JSON values.
[00:02:24]
One possible JSON value is an int, another is a bool, another is an object, another is an array. There's a whole bunch of different types of things that could be inside a JSON string. And when we ask Elm to decode that, we're saying, hey, I want you to turn this into some Elm type.
[00:02:42]
That's always an operation that can't fail. Anything we give it we say, hey, I want you to decode this string, and I want it to decode it has an int. That's effectively the same thing as what we did with String.toInt. We're effectively saying, I think that there is an int inside this JSON string.
[00:03:01]
If there is, then give me that int, otherwise tell me that it didn't work out. Now the way that decodeString works is it doesn't quite return a maybe. It returned something called a result. And result has two variants, just like maybe, one is called Ok, which sort of corresponds to just in maybe.
[00:03:21]
They essentially have the same purpose. So if this works, then it's like, okay, I said decodeString, I think that there is an int inside there, here's the string. If that succeeded, give me that int, in which case is great. Here you are, you now have that. The other branch is called Err.
[00:03:39]
So instead of just and nothing, we have Ok and Err. And the only difference between maybe and result is that in the Err branch we get some additional info on what went wrong. So nothing essentially says hey, I got nothing. I don't have anything for you. It's up to you to figure out how to handle this case.
[00:03:58]
Err says, it didn't work, but I do have some information that might be useful to you. So in this case, it's going to give us an error that says something like, hey, I was looking for an integer, and I didn't find one, instead I found the string 42.
[00:04:12]
Now in a particular case of decoding a JSON integer, this is not going to be terribly useful error information. You can imagine if you have a pretty substantial amount of JSON with some nested objects, some nested arrays, you might want some additional error information. Saying, hey, when I was trying to parse this JSON into the shape that you said it was going to be in, here's what went wrong.
[00:04:32]
Sort of like a compiler error, except for runtime, and decoding JSON instead of compiling Elm code.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops