Skip to content

Add Sleep.seconds!#460

Merged
Anton-4 merged 1 commit into
roc-lang:mainfrom
dmisiuk:issue-409-sleep-seconds
Jul 25, 2026
Merged

Add Sleep.seconds!#460
Anton-4 merged 1 commit into
roc-lang:mainfrom
dmisiuk:issue-409-sleep-seconds

Conversation

@dmisiuk

@dmisiuk dmisiuk commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Adds Sleep.seconds!, closes #409.

Discussed on Zulip@Anton-4 preferred F64 over U64 so that one function covers whole and fractional durations without having to switch back to millis!.

Implementation

seconds! converts to milliseconds and delegates to the existing Host.sleep_millis!, so there are no host or glue changes.

The conversion is the only subtle part, so it lives in a pure top-level helper, millis_from_seconds, which keeps it unit-testable. F64.round_to_u64 crashes on negative, NaN, infinite, and out-of-range values, which isn't acceptable for a signature with no error channel. F64.to_u64_try rejects exactly that set, so the helper separates the cases by sign: a positive value reaching the Err branch can only be one that overflowed the millisecond range.

Edge-case behavior

This wasn't part of the Zulip discussion, so flagging it explicitly for review:

Input Behavior
1.5 sleeps 1500 ms — fractional part truncated to whole ms
0.0004 truncates to 0 ms rather than rounding up
0 returns immediately
negative returns immediately
NaN returns immediately
inf, or beyond U64.highest ms saturates at U64.highest ms

Returning immediately on negative and NaN follows Go's time.Sleep. Happy to change either behavior if you'd prefer something different.

Testing

Nine expect blocks in platform/Sleep.roc cover millis_from_seconds across all three branches — the ordinary conversion, the negative/NaN case, and saturation. Each was verified to fail when its expected value is altered, so they aren't vacuously green.

Full ./scripts/test.py passes — host build plus all examples.

The effectful wrapper was also measured against a local build of the platform before being reduced to a one-liner: 1.5 → 1500 ms, 0.25 → 253 ms, 0 → 0 ms, -5 → 0 ms, NaN → 0 ms. The saturating branch can't be exercised at runtime, since it sleeps for roughly 584 million years; it is covered by the expect on millis_from_seconds instead.

examples/time.roc now uses Sleep.seconds!(1) in place of Sleep.millis!(1000) and its # 1000 ms = 1 second comment, which is the ergonomic problem the issue describes. Its one-second-sleep case asserts output shape rather than the duration value, so scripts/test_spec.json is unchanged.

@dmisiuk
dmisiuk force-pushed the issue-409-sleep-seconds branch from a7eed63 to e8500e9 Compare July 24, 2026 16:14
@dmisiuk dmisiuk closed this Jul 24, 2026
@dmisiuk dmisiuk reopened this Jul 24, 2026
@Anton-4 Anton-4 self-assigned this Jul 25, 2026

@Anton-4 Anton-4 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dmisiuk!

@Anton-4
Anton-4 merged commit e2d909c into roc-lang:main Jul 25, 2026
39 of 41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add Sleep.seconds!

2 participants