Stream: beginners

Topic: Destructure a record and also get the whole record


view this post on Zulip Ostar (Aug 27 2024 at 08:47):

Hello everybody,

Is it possible to destructure a record to get field "a" and "b" into "a" and "b" variables and also get the whole record into "input" variable? Something like this?

# invalid code
stuff = \{ a ? "default1", b ? "default2" }@input ->
    (a, b, input)

I want that so I can recursively call the function with a different field:

stuff = ...
  ...
  # from within the function
  stuff { input & a: "bla bla" }

Thank you!

view this post on Zulip Brendan Hansknecht (Aug 27 2024 at 16:57):

There should be, but the syntax is currently broken

view this post on Zulip Brendan Hansknecht (Aug 27 2024 at 16:58):

It should be {a, b} as input

view this post on Zulip Brendan Hansknecht (Aug 27 2024 at 16:58):

You can destructure as the first line of the function instead

view this post on Zulip Brendan Hansknecht (Aug 27 2024 at 16:58):

stuff = \input ->
    { a ? "default1", b ? "default2" } = input
    (a, b, input)

view this post on Zulip Brendan Hansknecht (Aug 27 2024 at 17:02):

Also, just a prewarning: optional record fields have a really inconvenient bug currently. They only specialize to one selection of optionality (which kinda makes them useless).

This means:

# This works by itself
stuff {a: "something"}

# This still works cause it is also missing b
stuff {a: "something2"}

# This breaks when used with the above due to a compiler bug:
stuff {a: "something3", b: "roc is sad :cry:"}

view this post on Zulip Anton (Aug 27 2024 at 17:19):

Do you think this bug is the cause for #7031 and/or #7035?

view this post on Zulip Brendan Hansknecht (Aug 27 2024 at 18:40):

Yes


Last updated: Jul 06 2025 at 12:14 UTC