Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change dict syntax from {, ...} to :{...} #124

Merged
merged 3 commits into from
Dec 30, 2024
Merged

Conversation

nick-paul
Copy link
Collaborator

@nick-paul nick-paul commented Dec 30, 2024

Change

  • Current syntax: {, 1:a 2:b }
  • New syntax: :{ 1:a 2:b }
    • The new syntax also supports standard block headers: 1 2 :{x y, 3:z} => :{1:x 2:y 3:z}
  • Extended operators syntax changed from :{operator.name} to :(operator.name)

Reason for change: Improved interaction between dict literals and the stack

Want to create a dict using the top two items from the stack and assign them to keys x and y?

Here is how you accomplish this in 0.5

1 2 {2, :y; :x; }

Problems:

  • You need to specify the number of items you want from the stack
  • Adds another special case for block headers (a non-empty header with a single number is a dict, not a block)
  • You need to assign the variables in reverse order
  • You need to remember to pop (;) items so you can assign the next ones

Overall this feels clunky, the dict literal doesn't interact with the main structure of the language (the stack) well

With the new syntax, you can use a plain header like you would for a block. Here is code that creates the same dict literal from above:

1 2 :{x y, }

You can specify additional variables in the literal as you normally would

.# 0.5
1 2 {2, :y; :x; 3:z}

.# 0.6
1 2 :{x y, 3:z}

Block and dict headers have the same syntax (and use the same implementation internally) so you can use all header features in your dict literals that you would in blocks:

  • Arguments:
    • 1 :{a,} -> :{1:a}
    • 1 :{a, a:b} -> :{1:a 1:b}
  • Types
    • 1 :{a::num, } -> :{1:a}
    • 1 :{a::str, } -> Type Error
  • Captures
    • 1:a; :{: a^, } -> :{1:a}
  • Splatting
    • [1 2] 3 :{[a b] c, } -> :{1:a 2:b 3:c}

@nick-paul nick-paul merged commit 99875fc into master Dec 30, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant