Very basic question about record updating

I asked on another forum for recommendations for languages to get a handle on functional programming. One suggestion was Elm, and when I saw some examples of apps developed in Elm, it seemed like this might be a good language to learn in order to implement a simple board game I’ve been working on. So that is why I’m here learning Elm.

However I believe I have wandered into something a bit (very) over my head, in that I’m a relative beginner to coding in general. Every few years I dip a toe in, learn a thing, then leave. Last time was ten years ago and the thing I learned was what object oriented programming is like. So you can see I’m starting from very little here.

Well anyway I’ve run into a early wall. I don’t understand how to use updated records. I’ve read the tutorial here and one other more handholdy Elm learning doc. In both cases, I learn how to update a record, and in both cases am told this doesn’t modify the record’s value but instead returns a new record. So far so good! But the issue I’m having is, I don’t understand how to use that updated record! As far as I can tell I have no way to name it. After the line in which I use the update syntax, as far as I can tell, the updated version of the record is just gone. I don’t get to use it again.

Am I missing something, up to and including the point? I believe this is a really basic question probably and I appreciate any assistance you have the patience to offer! I’m starting to get the feeling from the documentation and discussion I’m seeing that Elm isn’t thought of as a beginners language and that a lot of the discourse around it assumes knowledge of some important meta arcana I am wholly noncognizant of!

You almost have it!

The easiest way to understand it is probably with the Browser.sandbox application. When you are writing your update function, it takes in the Msg (the event that occurred), and the current Model, and you return a new Model. You don’t explicitly name the new record, it is simply the Model returned from update.

update : Msg -> Model -> Model
update msg model =
    case msg of
        Increment ->
            { model | count = model.count + 1 }

        Decrement ->
            { model | count = model.count - 1 }

Once update runs it will call your view function, providing it the new Model that you returned. The app then waits for the next Msg to come into update, and the whole scenario repeats.

Okay thank you, maybe I just need to keep reading, as the arrow notation you’re using isn’t something I’ve encountered yet. What does it signify?

Programming with expressions (functional programming) is a bit different from programming with statements.

Your program (function) is basically one big expression where you create a new value based on all the input values.

If you need to name parts of your expression to use in the final expression you can use the let statement, like this:

myFunction record =
  let updatedRecord = { record | field = ... }
  expression using updatedRecord

The -> arrow is used for pattern matching, check out for some examples of it.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.