Search code examples
ocamlreserved-wordsreason

How to access a valid OCaml identifier from Reason that is a reserved keyword in Reason


Is there a way to use an OCaml identifier that is a reserved keyword in Reason?

Let's consider the following example:

(* some_library.ml (valid OCaml) *)
type pub = {
  x: string;
}

When I try to use the type pub from Reason,

/* MyApp.re (invalid ReasonML) */
type someTypeAlias = Some_library.pub;

a syntax error error is displayed because "pub" is a reserved keyword in Reason.

Error: 743: pub is a reserved keyword, it cannot be used as an identifier. Try `pub_` or `_pub` instead

If I try to use the suggested identifiers (_pub or pub_), it doesn't work, because these expect the same name in the OCaml file.

To be more specific, I'm trying to use pub type in Nocrypto library.

It would be nice if there was a way to escape a keyword in ReasonML or specify accessed name in OCaml... Something like

/* MyApp.re (draft/invalid ReasonML) */
type someTypeAlias = Some_library.[@reason.keyword_as_identifier "pub"];

Is there something like that? Or wouldn't it be possible to create some workaround using external? I'm not very excited about forking the library just to rename one type.


Solution

  • A simple workaround would be to add a type alias in an Ocaml-syntax wrapper:

     type notpub = pub = { x: string }