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

Object literal shorthand #238

Closed
edemaine opened this issue Jan 16, 2023 · 2 comments
Closed

Object literal shorthand #238

edemaine opened this issue Jan 16, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@edemaine
Copy link
Collaborator

edemaine commented Jan 16, 2023

[DONE] {foo.bar().baz}

The following has been implemented! (#276)

{foo} for {foo: foo} is nice. As discussed in Discord, let's extend to:

  • {foo.bar} for {bar: foo.bar}
  • {foo()} for {foo: foo()} (note: avoid conflict with methods with empty bodies)
  • {foo.bar().baz} for {baz: foo.bar().baz}

One catch is that this (specifically foo()) forbids methods with empty bodies. I think this is a useful trade-off, but we might want to figure out a nice empty body notation (perhaps do, as in foo() do).

[DONE] JSX without braces

All of these would automatically work as JSX attributes. Furthermore, they are all unambiguous without the braces in JSX, so should be supported without them (just like ...foo is currently shorthand for {...foo}). This is especially valuable for things like props.foo (meaning foo={props.foo}) or foo() (meaning foo={foo()}), both common patterns in SolidJS. But a bare foo would remain as-is, instead of foo={foo}.

This is now implemented! (#508)

[DONE] Glob notation

Related, we also discussed one of the following notations for common stems:

  • foo.{a,b,c} for {foo.a, foo.b, foo.c} i.e. {a: foo.a, b: foo.b, c: foo.c}
  • foo{a,b,c}
  • Or allow both (similar to ?.( and ?()

We can also imagine more complex chains like foo.{a,b}.{c,d}, which I imagine expanding to {foo.a.c, foo.a.d, foo.b.c, foo.c.d}. I'm less certain this is useful. But something like foo.{a,b.c} for {foo.a, foo.b.c} almost certainly would be useful.

This is now implemented! (#333, #331, #327)

[PARTLY DONE] Assignment globs

We should also allow foo.{a,b,c} = bar (and maybe foo{a,b,c} = bar) to mean foo.a = bar.a, foo.b = bar.b, foo.c = bar.c or perhaps Object.assign(foo, bar.{a,b,c}). Actually, {a: foo.a, b: foo.b, c: foo.c} = bar works, just like the non-assingment case.

We can support spreads in this case: foo.{a,b,...rest} = bar means {a: foo.a, b: foo.b, ...foo.rest} = bar.

Above is now implemented! (#333, #343)

When the right-hand side is a literal, so we already know what the keys are, perhaps we could allow foo.{} = {a: 1, b: 2} or foo. = {a: 1, b: 2} or foo.= {a: 1, b: 2}. More generally, we could implement foo.{} = bar via Object.assign(foo, bar), bar (but should really use refs).

@edemaine edemaine added the enhancement New feature or request label Jan 16, 2023
This was referenced Feb 4, 2023
@STRd6
Copy link
Contributor

STRd6 commented Feb 22, 2023

Would be nice to add refs for things like:

f().{x, y}
---
let ref;
ref=f(),{x: ref.x, y: ref.y}

@STRd6
Copy link
Contributor

STRd6 commented Mar 3, 2024

Looks like this is completed.

@STRd6 STRd6 closed this as completed Mar 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants