Stream: ideas

Topic: generating sequential unique IDs


view this post on Zulip Richard Feldman (Jun 18 2023 at 01:25):

I just realized it'd be possible to use the "record builder" syntax @Agus Zubiaga recently implemented to implement a generator for sequential unique IDs - something like this:

{ foo, bar, baz } = Count.from 0 {
    foo: <- Count.inc, # increment by 1
    bar: <- Count.inc,
    baz: <- Count.inc,
} |> Count.done

view this post on Zulip Richard Feldman (Jun 18 2023 at 01:26):

I did something similar in Elm a long time ago

view this post on Zulip Richard Feldman (Jun 18 2023 at 01:27):

basically this is a way to avoid doing like:

foo = 0
bar = 1
baz = 2

...and then having to manually keep them all sequential, check for duplicates, etc.

view this post on Zulip Richard Feldman (Jun 18 2023 at 01:29):

you could also replace Count.inc with a custom function which (for example) wraps the number in an opaque type like MyId or whatever

view this post on Zulip Richard Feldman (Jun 18 2023 at 01:29):

or transforms it however you like


Last updated: Jun 16 2026 at 16:19 UTC