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?
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
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)"
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?
im pretty sure i brutalized the roc syntax there, but hopefully you get what i mean
oh Brendan beat me to it
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)"
Loric Brevet said:
Oh that means it brings
x
andy
in scope? Or maybe the root scope is a record behind the scenes, and this addsx
andy
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.
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.
If I rephrase what it does: it extracts fields from a record into dedicated variables
Yeah, definitely can look weird when directly used. I think it looks a lot more natural in function arguments or pattern matching
Yes, at the first glance, your latest example with greet
seems more common effectively!
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?
Yep
This makes more sense in response to my initial question then, thanks a lot
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.
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 ...
That also avoids bloating the tutorial with unnecessary detail/things that can't be done.
also in the future dbg
can work at the top level, just not until we get compile-time evaluation of constants working :big_smile:
hmm. How would it work though? When would it actually run? Print at compile time?
yeah exactly
that's when any crash
es would print too, if they come up
Hey thanks for the feedback. The errors are fairly descriptive for dbg but the destructing returns a rust kernel panic.
It also crashes the code so you have to Ctrl+c out of the process.
could you file an issue with a small repro. That would be great to have tracked in the repo
Hello, would gladly do so, but should I do it on zulip or Github or where is most appropriate for you?
https://github.com/roc-lang/roc/issues/new
https://github.com/roc-lang/roc/issues/6472
Last updated: Jul 06 2025 at 12:14 UTC