At long last https://github.com/mulias/roctoberfest/tree/main/advent_2022/day_08
walk = \array, startState, options, fn ->
{
direction ? Forward,
orientation ? Rows,
start ? defaultStartIndex array direction
} = options
but I ran into a couple different compiler issues and couldn't get the value of start
to rely conditionally on direction
. I went with a different API for now.
shape
to get the dimensions of an Array2D
. that meant I couldn't use shape
as a variable in any other function. Doing the occasional gymnastics to not repeat variable names is not a deal breaker, but it's the most common papercut I encountered.UNRECOGNIZED NAME
error "The Foo
module does not expose anything by the name bar
" does not give a line number for the issue and can be a bit tedious to debug.For the optional deconstruction, does it work if you do it all in the one line within the function? From memory I think there was a bug with the parser that meant it worked on one line but across multiple lines it didn't. So walk = \array, startState, {direction? Forward,orientation? Rows,start? defaultStartIndex array direction}, fn ->
?
I'm not sure I have seen optional field destructuring as a statement, i.e. within the body of a function and not the arguments, that looks like a really useful feature. I'm not sure but maybe we should raise a thread in #ideas for it? Or maybe that is already the intended use for that, in which case it would be good to add an explanation to the Tutorial.
Actually, I just realised.. I doubt you can use the array
argument and call a function when providing a default value... :oops:
I should really make an example with the different errors that came up, but yes having to be on one line was confusing. The formatter forces it which helped figure that one out.
This is currently working fine
WalkOptions : {
direction ? [Forward, Backwards, ForwardFrom Index, BackwardsFrom Index],
orientation ? [Rows, Cols],
}
walk : Array2D a, state, WalkOptions, (state, a, Index -> state) -> state
walk = \array, startState, options, fn ->
{ direction ? Forward, orientation ? Rows } = options
The main thing I ran into was that the default value for start
couldn't use the value for default
, which right now is specifically mentioned in the tutorial as something you can do.
This section has an example where a default value is set with a function and a reference to a different value getting destructured https://www.roc-lang.org/tutorial#optional-record-fields
{ height, width, title ? "", description ? Str.concat "A table called " title }
For reference, shadowing has been thoroughly debated.
Last updated: Jul 06 2025 at 12:14 UTC