Use keyword as identifier

I propose a way of writting identifier tokens, preventing them as being keyword tokens. Useful for lexical references, function parameters and so on, where string literals cannot be used. I suggest one of following syntaxes:

  • 'for'i (string literal treated as identifier)
  • \i:for (backslash)
  • \r:for (backslash)
  • \#for (backslash)

I've adopted a convention from Python's coding standard in my own code to get around these issues. Python recommends you place a trailing underscore on variable names that are otherwise reserved by python, or that would otherwise shadow a global Python function. So, in JavaScript I'll write code like this:

function getId(for_) {
  // ...

And, really, having a trailing underscore isn't that different from having a variable that you always have to refer to with \#for syntax, in many cases, the \# sort of just becomes part of its name, since that's the only possible way to refer to it.

1 Like

Does not sound a bad convention. The advantage of escaping though is generating type docs without underscore included.

Yeah, that's a good point.

Though, I could see editors choosing to include the escape in the type-doc strings anyways, because a function signature might look a little confusing if the parameters were unescaped.

For example, this looks a bit odd.

function doThing(return: string): void

So, they might choose to render it like this anyways:

function doThing(\#return: string): void

At which point, we're once again not doing anything better than a trailing underscore:

function doThing(return_: string): void

In some cases, they might even be forced to render the \#, for example, if you named your parameter \#this, they have no choice but to render it as function doThing(\#this: Thing): OtherThing, because an unescaped this commonly means something else - it means it expects you to .call() the function with a specific this parameter of a specific type.

1 Like