keithamus / proposal-object-freeze-seal-syntax

A JavaScript TC39 proposal for Object.freeze & Object.seal syntax
https://www.keithcirkel.co.uk/object-freeze-seal-syntax
59 stars 8 forks source link

Alternative Syntax Ideas #21

Closed guybedford closed 5 years ago

guybedford commented 5 years ago

I was just wondering if {{ name: 'value' }} might be a viable syntax here?

Even 3 brackets look fine - eg {{ sealed }}, {{{ frozen }}}?

keithamus commented 5 years ago

Hey @guybedford thanks for the issue!

I am fully open to discussing new syntaxes - it is not at all tied down, but I have some reasons for the current decisions.

To give some context on the existing syntax; Flow has a sealed object syntax which looks like {| |} - this proposal mirrors that, and goes a bit further with {# #}. I'm not married to the syntax and I'm happy with whatever syntax we chose - as long as it is ergonomic, both reading and writing.

The existing syntax speaks to me in terms of semiosis; I kind of like {| |} for seal because the |s look like little walls, so its reads to me as a "walled object, nothing can go beyond whats already inside", in a similar fashion # kind of reminds me of a frozen ice cube so the {# #} syntax just jumps out to me as "frozen" object. Hopefully I'm not the only one who has these intuitions.

To talk about your syntax proposal, I think having two {s does not share the same semiotics. Consider foo => ({ wrapped: foo }) vs foo => ({{ wrapped: foo }}) vs foo => ({{{ wrapped: foo }}}), they look similar but do different things - this is mostly a pedagogical concern.

There are also syntax concerns though. {{ has valid grammar today, e.g. foo => {{ foo }} is a valid syntax today - even though its perhaps erroneous code it doesn't have any syntax errors. The ambiguity here is, I imagine, a spec concern.

Lastly, I'd like to consider that part of the extended proposal also looks to add sealed function syntax (so ((| x |) => x*2)(1, 2) would be a runtime error). I'd like to make sure we keep a view for this extension - and while some my argue that "sealed object" vs "fixed arity function" are different semantics, my argument is that they share intent and so a symmetry is there. Using {{ }} for sealed object means we cannot have this symmetry because ({ x }) => is valid syntax, and my hunch is ({{ x }}) => will run into issues.

I'll leave this issue open because I do want to consider alternative syntaxes, but I hope the above provides a useful rubric.

guybedford commented 5 years ago

Of course that this is a valid grammar already is pretty much a non-starter I think. Thanks for the clear response on this.

Could {|| ||} perhaps be a syntax option to go along with {| |}?

guybedford commented 5 years ago

Actually # seems easier I guess and not that different! Will close this one out, and thanks for the discussion and your work on this.