How do i defualt a date model variable to todays date

When I started with Elm, I‘ve run into a similar problem: The init function runs only once, so why bother with tasks and subscriptions to do a simple thing like getting today’s date—especially because you can be fairly certain that the date doesn’t change during one session?

That put me off a little since in other languages it’s such a simple operation. The key for me was to realize that:

a) There is actually no guarantee that the init function is called only once. It’s perfectly fine to call it later from my own code, e.g. to reset the model to an initial state. And then the guarantee that functions always return the same values when called with the same parameters (none in this case) would be broken.

b) The date might actually change during one session. It happens every day. So why would my code assume that it doesn’t?

Keeping all functions side-effects free gives me all the power of Elm and the downsides of making init (or Date) special simply aren’t worth it.

Maybe it’s obvious or long winded. I’m just recounting my thought process back when I began using Elm. Hope it helps.

4 Likes