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

Typing of object/array pattern components via :: #1383

Merged
merged 4 commits into from
Aug 29, 2024
Merged

Conversation

edemaine
Copy link
Collaborator

Fixes #126 by implementing a long-sought syntax for typing individual object properties and array elements in left-hand-side patterns (including function arguments and assignment). Basically, :: type can come at the end of any existing object property or array element. For example:

function Component({
  name: [first:: string, last:: string, ...rest:: string[]],
  counter:: number
  setCounter: sc:: (number) => void
})
↓↓↓
function Component({
  name: [first, last, ...rest],
  counter,
  setCounter: sc
}: {
  name: [ string, string,... string[],],
  counter: number,
  setCounter: (number) => void,}){}

Why this notation?

  • name:: type: value is ambiguous: it could be interpreted as name:: {type: value} (typing as object). (I tried to include this as an option at first, but ran into this alternate parse.)
  • name: value: type is similarly ambiguous: it could be interpreted as name: {value: type} (complex destructuring).

I admit I still often type the wrong number of :s when writing examples, but I imagine we'll get used to it. (Also open to better proposals for notation.)

I still need to write actual docs, but hopefully the example above and the tests give a reasonable idea.

Copy link
Contributor

@STRd6 STRd6 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, I like the feature! The explanation of the challenges with the other approaches makes sense to me.

I added a few nitpicks but don't let them hold this up if it's too tedious.

source/parser.hera Outdated Show resolved Hide resolved
source/parser/binding.civet Outdated Show resolved Hide resolved
typeSuffix .= elem.typeSuffix
typeSuffix ??= elem.binding?.typeSuffix
count++ if typeSuffix
typeElement .= [ typeSuffix?.t, "," ]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should only add trailing commas as authored in the source. It's not necessary for this to merge but ideally trailing commas would always be as authored (some parts of the parser currently do this better than others).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for encouraging this; it turned out to be pretty easy to improve upon using delim properties (just has to add them in one case). It's still not exactly perfect; there's an added comma at the end of an object when a ...rest part got extracted out so it wouldn't be necessary. But it seems close enough.

Leaving better preservation of whitespace to future work. It's pretty good now, but definitely not perfect.

@edemaine edemaine merged commit 6713fdd into main Aug 29, 2024
3 checks passed
@edemaine edemaine deleted the types-in-objects branch August 29, 2024 14:31
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.

2 participants