Labels punning #3146
Replies: 3 comments 7 replies
-
Hello! This is the first time I've heard this being suggested for expressions as well as patterns. Was that included in the design so that expressions mirror patterns? Or do you think there are strong motivations for expanding the expression language too? You've only given motivations for patterns here. Do we have any other suggestions for syntax? |
Beta Was this translation helpful? Give feedback.
-
Not really suggesting one particular style, but this survey I put together has examples of punning from a few different languages and could be useful for comparison. |
Beta Was this translation helpful? Give feedback.
-
This seems similar to the syntax added to Ruby in v3.1 which is nice at times but ugly at other times. In my opinion, it seems like an expert friendly feature that would make Gleam codebases harder to reason about for people just starting out with the language. You also run into the following formatting problem occasionally in Ruby when using it with kwargs or when creating hashes. do_something(
one:,
two:,
three:,
four: another_name,
five:,
six:,
) If it is restricted to only pattern matching, I can see a better argument for it. I think having a reduced scope would make it less confusing. |
Beta Was this translation helpful? Give feedback.
-
I think Gleam would benefit from allowing label punning, that is a shorthand way to deal with labelled arguments.
What we currently do if we want to use labelled arguments in pattern matching looks like this:
When pattern matching (or building a record), using labels instead of relying on the arguments' position can be a great way to avoid subtle bugs that would pop up in case of refactorings to the type definition. Imagine we did this:
And then the original definition changed to:
The type system wouldn't be able to catch this subtle bug and we would have swapped
wibble
andwobble
in the pattern matching I showed in the example above.However, nothing would break if we used labels instead.
The problem is that having to repeat the name of the label twice can get quite annoying (and I must admit I usually end up using positional arguments instead because I can't be bothered writing everything twice).
Of course I could just use shorter names to bring labelled arguments into scope:
That would for sure solve my problem of repeating the label name twice but now my code using those arguments will be harder to read and littered with hard to understand variable names, I do want to use the label name!
While I'm aware that Gleam optimises for explicitness over terseness, I think this would be a really nice addition to make using labelled arguments less painful.
What would this look like
These definitions will be used in the following examples:
Record pattern matching
Just providing the label followed by
:
would mean the same as writing<label-name>:<label-name>
, bringing it into scope:Record construction
Passing
<label-name>:
to a record constructor would be the same as writing<label-name>:<label-name>
:Since that's just some shorthand notation to pass in a labelled argument with the same name as the label all the same rules would apply.
Function calls
@hayleigh-dot-dev pointed out that the same behaviour could apply uniformly to labelled function arguments, not just constructors (since those are just functions themselves):
Once again, all the same rules that already exist for labelled arguments would apply here as well (no unlabelled arguments after labelled ones, ...)
Other things to point out
Here's some considerations I think are worth pointing out from Discord's discussion thread:
:<label-name>
because that's Elixir's syntax for defining an atom and that would end up being quite confusing for Elixir folks trying Gleam out:
after the label name would also make things easier to refactor if one wants to change the name of the labelled argument: just slap a name after itBeta Was this translation helpful? Give feedback.
All reactions