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

Bulleted arrays with . or #1361

Merged
merged 3 commits into from
Aug 15, 2024
Merged

Bulleted arrays with . or #1361

merged 3 commits into from
Aug 15, 2024

Conversation

edemaine
Copy link
Collaborator

Fixes #803

@bbrk24
Copy link
Contributor

bbrk24 commented Aug 14, 2024

Can you add as tuple or as const (or any other type cast, really) to a bulleted array without adding parens?

@edemaine
Copy link
Collaborator Author

Good question. No, it's not possible without parens. I think at least |> as tuple and |> as const should be addable, but even those don't work yet.

I think trailing type casts (as ...) in the same places that trailing member/call expressions (and maybe pipes from #1168) would make sense as a separate issue.

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.

Thanks for implementing this feature. I'm a little bit scared so I think that means we should do it.

@edemaine
Copy link
Collaborator Author

I definitely feel like I'm stretching the grammar a bit here. We lack a nice place to put things that work only at the indentation level, not deeper in an expression. For example, I didn't want to allow something like this:

array.map . foo
          . bar

(in particular because array.map . foo already has a meaning, involving placeholders)

As a result, it seems like I had to add (Nested)BulletedArray in a few too many places. Perhaps this could be cleaned up somehow... I'm open to ideas!

Related, I just added support for trailing members/calls and pipelines after top-level bulleted arrays, enabling |> as const. I'm not sure I added it to all the right contexts though...

I think this is a reasonable starting point though, and we can extend/fix as we run into further issues.

@edemaine edemaine merged commit 022a39f into main Aug 15, 2024
4 checks passed
@edemaine edemaine deleted the bullets branch August 15, 2024 17:41
@danielbayley
Copy link
Contributor

@edemaine Surely YAML-like - would make more sense here than ., which feels a bit too close to…

object
  .method()
  .chaining()

@edemaine
Copy link
Collaborator Author

@danielbayley How would you distinguish from subtraction?

@danielbayley
Copy link
Contributor

@danielbayley How would you distinguish from subtraction?

Iv’e never personally used, or seen any multiline subtraction expressions in the wild. Which is of course, not to say this edge case doesn’t exist…

Maybe the distinction could be that if there is a newline between - entries, then it’s an array, else -s on the same line is interpreted as subtraction. Then if you did actually want multiline subtraction, you could just wrap that in parentheses? Something like:

total := (
  n
  - 1
  - 1
)

Although, maybe that isn’t even necessary, given that in order to subtract, the first token after the assignment = would be a value to subtract from anyway…

@edemaine What do you think?

@edemaine
Copy link
Collaborator Author

edemaine commented Aug 16, 2024

My worry is examples involving indented function calls, like this:

func x
  - y
  - z

// parse 1: subtraction (how Civet works now)
func(x
  - y
  - z)

// parse 2: call with array argument (how . works now)
func(x(
  [ y,
    z]))

I guess you're saying to prioritize the latter parse, but I don't see how to obtain subtraction behavior without moving the - signs onto the previous line (as requierd in CoffeeScript, but something I really don't like about CoffeeScript). Wrapping in parentheses doesn't help.

@edemaine
Copy link
Collaborator Author

One possibility that comes to mind: We could add an opt-in YAML compatibility mode via "civet yaml" (or some better name) that

  • enables - and * as bullets, but also
  • prevents - and * from being used as binary operators when preceded by a newline.

For some, I can imagine this is a worthwhile trade-off.

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.

Bulleted arrays
4 participants