How can i acheive the following Elm?

Hello everyone, I’m trying to create a function to generate id. For Example 1, 2, 3, … so on Not UUID. Just simple number.
In JS we can write a function like following to achieve this

function () {
   let count = -1
   return {
         getNext: () => {
                count = count + 1
                return count
         }
      }
}

how can we achieve something similar in elm. I tried to look into Random module to see how random keeps track of state but i dont understand anything.
Can somebody help me?. I have a solution to keep a counter in the state and keep incrementing. Is it the right way to do it. Or is there a better way ?

Yes, keeping a counter in the state/model and incrementing it is the way to go!

If you find that you keep forgetting to increment the counter, you could make a module that does clever things that forces you to update the counter while reading from it. But no need to complicate things until you run into bugs!

In general, if you want to update something over time you have to store it in your Model. That’s the only way of keeping state.

Thanks a lot!! :grinning:

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