elm / core

Elm's core libraries
http://package.elm-lang.org/packages/elm/core/latest
BSD 3-Clause "New" or "Revised" License
2.8k stars 359 forks source link

Add programStartTime to Time module #172

Closed TheSeamau5 closed 8 years ago

TheSeamau5 commented 9 years ago

When working with random numbers, it's often inconvenient to have to go to Signal land because the seed has to be generated from a Signal (say, the current time). While this is certainly a necessary way of doing things, sometimes you don't want a stream of random seeds, you just want one, once, but you want it to be different each time you execute a program anew or refresh a page.

I recall hearing this idea somewhere in the mailing list before but I can't find where but the idea is to add a value called

programStartTime : Time

which is the time when main was called. Since there is only one such value, there is no need for it to be in a signal. This would simplify design by removing some of the needs for signals in programs (and thus further pushing effects to the edge).

(sorry if this idea was already proposed but I couldn't find it in the issue tracker)

evancz commented 9 years ago

I've got some changes to Signal percolating that'd make this really easy and theoretically supported. This was a specific case I had in mind, specifically for randomness. So this should be in 0.15 with the promise stuff. Don't let me forget!

TheSeamau5 commented 9 years ago

Cool! Sure thing, I'll remind you when the release approaches.

jvoigtlaender commented 9 years ago

Just for reference, there is a commit that adding such a constant here. It's in a branch I didn't create a PR from, since I expected it wouldn't be merged anyway.

An important aspect to take care of (and that's why I wrote that branch/commit, to see how a solution could look like) is that this supposed "constant" remains constant even over a hot-swap. Even after a hot-swap, you still want to return the time of the original program start, not the how-swap time. Otherwise, referential transparency is lost.

TheSeamau5 commented 9 years ago

Yeah, the compiler might want to flag it in some way as "unhotswappable".

jvoigtlaender commented 9 years ago

Well, the commit I linked to does ensure that the program remains hotswappable. Even after a hotswap, still the original program start time is reported. Which is the semantically correct thing to do. All I was saying is that any way of introducing the requested feature should make sure to consider this issue. Mine does. :)

TheSeamau5 commented 9 years ago

Cool! That's just music to my ears. I can't wait for this to be a thing in Elm.

joshy commented 8 years ago

Any update on this? The documentation on the Random module says that in order to get a unexpected seed is to use the current time.

A good way to get an unexpected seed is to use the current time. No code example is given how to get the current time with just the core libraries. If there is a easy way, I can make a pull request with an code example.

jvoigtlaender commented 8 years ago

There's no way currently just using core. To get at the current time, an excurse to JavaScript is needed. Either explicitly by passing the time in through a port, or by using the Task API: http://package.elm-lang.org/packages/evancz/task-tutorial/1.0.3/TaskTutorial#getCurrentTime. (I think the latter is what Evan was referring to above.)

noahzgordon commented 8 years ago

I believe this is no longer relevant as of 0.17, because Time 4.x exposes now.

evancz commented 8 years ago

I like that interpretation. We'll see if this gets requested again!