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

Allow shorthand object literals #185

Closed
jcubic opened this issue Oct 21, 2021 · 3 comments
Closed

Allow shorthand object literals #185

jcubic opened this issue Oct 21, 2021 · 3 comments
Labels
enhancement New feature or request

Comments

@jcubic
Copy link
Collaborator

jcubic commented Oct 21, 2021

It would be nice to be able to use syntax like this:

(let ((x 1) (y 2))
  &(:x :y))

It may require a bit of work because right now object-expander uses pair-map that iterate over pairs and check if they are key? symbols. There is a need for a more general iteration that is key? aware and skip if there are no values.

This will be important to implement a complex let-bind macro ##184.

@jcubic jcubic added the enhancement New feature or request label Oct 21, 2021
@jcubic
Copy link
Collaborator Author

jcubic commented Oct 21, 2021

It seems this is impossible to do. Because & is a parser extension and expands on parse time into an object. And x y are runtime values.

The only thing that can be done is to allow undefined values, it probably is only useful with let-bind because object literals are read-only.

@jcubic
Copy link
Collaborator Author

jcubic commented Oct 22, 2021

Now shorthand object literals are never read-only:

(let ((obj &(:x :y)))
  (set! obj.x 10)
  (set! obj.y 20)
  obj)

@jcubic
Copy link
Collaborator Author

jcubic commented Oct 22, 2021

There is one problematic unit test:

(test "core: it should throw when change object literals long property after short property"
      (lambda (t)
        (let ((obj &(:x :y 20)))
          (set! obj.x 10)
          (t.is (to.throw (set! obj.y 30)) true)
          (t.is obj &(:x 10 :y 20)))))

if the first property is short the object is not frozen so you can still modify the second property. Maybe it's worth making read-only properties instead of the whole objects.

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

1 participant