Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typesafe time units: proc sleep(t: Duration), allowing sleep(1.second), sleep(1.millisecond) etc #617

Closed
timotheecour opened this issue Feb 23, 2021 · 3 comments
Assignees

Comments

@timotheecour
Copy link
Owner

timotheecour commented Feb 23, 2021

as noted in nim-lang#17149 (comment), sleep is confusing:

  • posix.sleep takes seconds
  • os.sleep takes milliseconds

this is confusing, not self documenting, error prone and not particularly typesafe

proposal

add proc sleep(t: Duration)
example:

sleep(1.second)
sleep(1.millisecond)
sleep(100.millisecond + 1.second)
sleep(1.5.hour)

note

std/times is a heavy dependency, ideally we'd be able to do this without depending on std/times, refs nim-lang/RFCs#55, via a new std/durations module (imported by std/times)

note

TimeInterval is not good, this has overhead because it's calendar based
we can define millisecond, second (etc) without s to avoid confusion with the existing milliseconds + friends.
eg:

proc second*(a: int | float): Duration
proc millisecond*(a: int | float): Duration
# etc
@timotheecour timotheecour changed the title typesafe time units: sleep(1.seconds), sleep(1.milliseconds) etc typesafe time units: proc sleep(t: TimeInterval), allowing sleep(1.seconds), sleep(1.milliseconds) etc Feb 23, 2021
@timotheecour timotheecour changed the title typesafe time units: proc sleep(t: TimeInterval), allowing sleep(1.seconds), sleep(1.milliseconds) etc typesafe time units: proc sleep(t: Duration), allowing sleep(1.second), sleep(1.millisecond) etc Mar 20, 2021
@ringabout
Copy link
Collaborator

ringabout commented Apr 24, 2021

Now, we could have

sleep(1'second)
sleep(1'millisecond)
sleep(100'millisecond + 1'second)
sleep(1.5'hour)

Consider following APIs

sleepFor

proc sleepFor(t: Duration)

The thread blocks for at least specified duration.

sleepUntil

proc sleepUntil(t: DateTime)

The thread blocks until specified time arrived.

@ringabout ringabout self-assigned this Apr 24, 2021
@timotheecour
Copy link
Owner Author

timotheecour commented Apr 24, 2021

Now, we could have

yes.

sleepFor, sleepUntil

maybe sleepMin, sleepMax is less ambiguous? but also, can you explain the semantics of those 2 procs, and in which case you'd end up sleep more than sleepFor or less than sleepUntil?

also: std/durations should be separate from std/times and be imported/re-exported from std/times
(because std/times is a monster already and durations can be selfcontained and small); related to nim-lang/RFCs#55

@timotheecour
Copy link
Owner Author

closing, as I'm opening the corresponding RFC nim-lang/RFCs#383 (because I'm seeing PRs that introduce duration params as int)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants