Stream: beginners

Topic: destructuring records in defs


view this post on Zulip Loric Brevet (Feb 25 2022 at 16:20):

Hey, I am struggling to understand this from the tutorial.

Finally, destructuring can be used in defs too:

{ x, y } = { x: 5, y: 10 }

What is the use-case for this? How can {x, y} be used afterward?

view this post on Zulip Brendan Hansknecht (Feb 25 2022 at 16:26):

This is equivalent to:

record = { x: 5, y: 10}
x = record.x
y = record.y

So x is just an int containing 5 and y an int containing 10

view this post on Zulip Emi (Feb 25 2022 at 16:28):

disclaimer: i'm inexperienced with roc, so i'm explaining this as it works in other languages, unaware of nuance that Roc might have

the statement you posted is equivalent to

some_rec = { x: 5, y: 10}
x = some_rec.x
y = some_rec.y

normally you wouldn't use it with a record you just created, because you already know what the values of all the fields are, but you might use it with, for example, an argument from a function. for example:

Person : { first_name : Str, last_name : Str, honorific : Str }

greet : Person -> Str
greet = \person ->
    let {first_name, last_name, honorific} = person
    "Hello \(honorific) \(last_name)"

view this post on Zulip Loric Brevet (Feb 25 2022 at 16:28):

Oh that means it brings x and y in scope? Or maybe the root scope is a record behind the scenes, and this adds x and y to this "root" record?

view this post on Zulip Emi (Feb 25 2022 at 16:28):

im pretty sure i brutalized the roc syntax there, but hopefully you get what i mean

view this post on Zulip Emi (Feb 25 2022 at 16:28):

oh Brendan beat me to it

view this post on Zulip Brendan Hansknecht (Feb 25 2022 at 16:30):

Can even take the person example a step further. This should work:

Person : { first_name : Str, last_name : Str, honorific : Str }

greet : Person -> Str
greet = \{first_name, last_name, honorific} ->
    "Hello \(honorific) \(last_name)"

view this post on Zulip Emi (Feb 25 2022 at 16:30):

Loric Brevet said:

Oh that means it brings x and y in scope? Or maybe the root scope is a record behind the scenes, and this adds x and y to this "root" record?

if i understand what you're saying, i think it's much simpler than that. it declares a completely new x and y variables in the local scope and copies the values from the struct into them.

view this post on Zulip Loric Brevet (Feb 25 2022 at 16:32):

Ok, what bothered me was the syntax being similar to a record. It is weird in this example that something that looks like a record: {x , y} end up in two local variables x and y. At least it is not really intuitive! Thanks for the explanation. I think this would be a great addon to the tutorial.

view this post on Zulip Loric Brevet (Feb 25 2022 at 16:34):

If I rephrase what it does: it extracts fields from a record into dedicated variables

view this post on Zulip Brendan Hansknecht (Feb 25 2022 at 16:36):

Yeah, definitely can look weird when directly used. I think it looks a lot more natural in function arguments or pattern matching

view this post on Zulip Loric Brevet (Feb 25 2022 at 16:38):

Yes, at the first glance, your latest example with greet seems more common effectively!

view this post on Zulip Loric Brevet (Feb 25 2022 at 16:40):

And so in that function example, does it mean that behind the scenes, the compiler also extracts from a record to local variables and brings them into scope for the function?

view this post on Zulip Brendan Hansknecht (Feb 25 2022 at 16:41):

Yep

view this post on Zulip Loric Brevet (Feb 25 2022 at 16:43):

This makes more sense in response to my initial question then, thanks a lot

view this post on Zulip Oliver (Jan 29 2024 at 23:15):

Hey I just came across this, and it would be nice if the tutorial included that destructuring and dbg do not work when implementet top-level, they need to be put into functions.

view this post on Zulip Brendan Hansknecht (Jan 29 2024 at 23:23):

Probably we should just have a good error message for it. Then when it is inevitably tried, users will get an explanation of why it can't compile: I see you tried to use "dbg" at the top level. dbg needs to be ...

view this post on Zulip Brendan Hansknecht (Jan 29 2024 at 23:24):

That also avoids bloating the tutorial with unnecessary detail/things that can't be done.

view this post on Zulip Richard Feldman (Jan 30 2024 at 00:01):

also in the future dbg can work at the top level, just not until we get compile-time evaluation of constants working :big_smile:

view this post on Zulip Brendan Hansknecht (Jan 30 2024 at 00:02):

hmm. How would it work though? When would it actually run? Print at compile time?

view this post on Zulip Richard Feldman (Jan 30 2024 at 00:26):

yeah exactly

view this post on Zulip Richard Feldman (Jan 30 2024 at 00:27):

that's when any crashes would print too, if they come up

view this post on Zulip Oliver (Jan 30 2024 at 08:11):

Hey thanks for the feedback. The errors are fairly descriptive for dbg but the destructing returns a rust kernel panic.

view this post on Zulip Oliver (Jan 30 2024 at 08:13):

It also crashes the code so you have to Ctrl+c out of the process.

view this post on Zulip Brendan Hansknecht (Jan 30 2024 at 16:09):

could you file an issue with a small repro. That would be great to have tracked in the repo

view this post on Zulip Oliver (Jan 30 2024 at 16:16):

Hello, would gladly do so, but should I do it on zulip or Github or where is most appropriate for you?

view this post on Zulip Brendan Hansknecht (Jan 30 2024 at 16:16):

https://github.com/roc-lang/roc/issues/new

view this post on Zulip Oliver (Jan 30 2024 at 19:02):

https://github.com/roc-lang/roc/issues/6472


Last updated: Jul 06 2025 at 12:14 UTC