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

Potential syntax alternative (postfix exclamation mark) #300

Closed
glen-84 opened this issue Apr 24, 2022 · 11 comments
Closed

Potential syntax alternative (postfix exclamation mark) #300

glen-84 opened this issue Apr 24, 2022 · 11 comments

Comments

@glen-84
Copy link

glen-84 commented Apr 24, 2022

Since #10 is limited to 3 alternatives, has a postfix exclamation mark (!) been considered? (it's not easy to search for, so I apologize if it has)

const x = [1, 2, 3]!;
  • It's currently a SyntaxError in JavaScript.
  • I don't think that it would conflict with TypeScript's non-null assertion operator, since a literal array or object could never be null.
@nicolo-ribaudo
Copy link
Member

nicolo-ribaudo commented Apr 24, 2022

The problem with postfix operators is that JavaScript's syntax is english-based and thus read left-to-right Being an array or a tuple is a fundamental difference and it affects the array/tuple contents, so when reading the code you would have to:

  • See the [
  • Visually skip to the closing ], making sure to skip "balanced [/] pairs"
  • Check if it's followed by a !
  • Go back to the opening !, and now you can read the contents knowing if they can only be primitives or also objects, and (for objects/arrays) knowing if methods and symbol keys are allowed or not.

@glen-84
Copy link
Author

glen-84 commented Apr 24, 2022

I'm not sure if this is really critical in practice. How long is the average/median record/tuple? Sure, there will be some cases of long records/tuples.

const proposal = {
  id: 1234,
  title: "Record & Tuple proposal",
  contents: `...`,
  // tuples are primitive types so you can put them in records:
  keywords: ["ecma", "tc39", "proposal", "record", "tuple"]!,
}!;

If you set one of the properties to an object, your IDE would alert you immediately (yes yes, not always using an IDE, code review, etc.).

It's not a perfect solution, but it may be worth considering.

The only other idea I had was a subtle change to the current {| ... } option, moving the pipe symbol outside the braces, to make it seem a bit less asymmetrical ...

const proposal = |{
  id: 1234,
  title: "Record & Tuple proposal",
  contents: `...`,
  // tuples are primitive types so you can put them in records:
  keywords: |["ecma", "tc39", "proposal", "record", "tuple"],
};

Essentially, using the pipe symbol instead of using # or @, which are already used for other purposes, and would make code look quite confusing ...

class Example {
  #field = #{id: 1234}; 😕
  @dec field = @{id: 1234}; 😕
}

@mhofman
Copy link
Member

mhofman commented Apr 24, 2022

Postfix exclamation, besides the issues raised earlier, is pretty much a non starter for anything that wouldn't be a runtime non-nullish assertion, because of Typescript's own static non-null assertion operator that is used in the wild.

@ljharb
Copy link
Member

ljharb commented Apr 24, 2022

To be clear - not because of compat, because TS doesn’t run natively anywhere on the web - but likely because of documentation and tutorials and the user confusion it’d cause.

@glen-84
Copy link
Author

glen-84 commented Apr 24, 2022

I covered that in the OP. You don't non-null assert a literal.

@mhofman
Copy link
Member

mhofman commented Apr 24, 2022

It's used in the same places syntactically. The confusion to both humans and parsers would be tremendous.

@glen-84
Copy link
Author

glen-84 commented Apr 24, 2022

I'm not sure how this would confuse parsers. It would mean the same thing in both JavaScript and TypeScript when appended to an array/object literal.

Humans could be slightly confused by the distinction. I wouldn't go as far as tremendously confused. 😄

Anyway, how about the |{ ... } option – are there any parser issues with this?

@mhofman
Copy link
Member

mhofman commented Apr 24, 2022

Technically yes because of bitwise OR

@glen-84
Copy link
Author

glen-84 commented Apr 24, 2022

Bitwise OR requires two operands though?

@acutmore
Copy link
Collaborator

let x = y|[1,2,3].at(index);

Is already valid syntax, which means the tokeniser can't emit |[ as a single token. Whereas #[ can be.

@glen-84
Copy link
Author

glen-84 commented Apr 24, 2022

I see. I thought that it could tokenize it differently depending on its position. Clearly I don't now enough about tokenization. 😝

From the current options, my preference is {| ... } (or {| ... |}).

@glen-84 glen-84 closed this as completed Apr 24, 2022
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

No branches or pull requests

5 participants