Allowing reserved words in polymorphic variants

I’m working on porting my websocket implementation from reason to rescript, and remembered how much this annoyed me:

@bs.send
external on: (
  t,
  @bs.string
  [
    | #close((int, string) => unit)
    | #message(string => unit)
    | @bs.as("open") #open_(unit => unit)
    | #ping(Node.Buffer.t => unit)
    | #pong(Node.Buffer.t => unit)
    | #error(Dom.errorEvent => unit)
  ],
) => t = "on"

Is there a reason that polymorphic variants can’t use reserved words, since afaik they’re always marked by a #? Or is it something that just hasn’t been deemed necessary.

2 Likes

Indeed I think that the parser should potentially be able to be disambiguate this. Since ReScript is now owning their own parser is this something that can be improved in the future?

You can write #\"open". See https://rescript-lang.org/docs/manual/latest/use-illegal-identifier-names for more context.

In the future we will support nicer syntax and allow reserved words without the escaping mechanism. But this will likely take a while.

6 Likes

Seeing as how you’d have to use the backslash at use site as well, this isn’t much better than just using #open_, unless I’m mistaken. Ah well.