dfinity / motoko

Simple high-level language for writing Internet Computer canisters
Apache License 2.0
506 stars 97 forks source link

IDL reserved keywords in target language #826

Closed chenyan-dfinity closed 3 years ago

chenyan-dfinity commented 4 years ago

In Motoko, we can define type record = Nat;, then this gets translated to type record = nat; in IDL. This cannot be parsed in the IDL compiler, as record is a keyword in IDL, but not in Motoko.

The same problem happens for IDL -> JS, we can define type const = nat; in IDL, then the generated JS binding cannot be parsed, because const is a keyword in JS, not in IDL.

I think we need an escape mechanism for translating identifiers: append _ in the identifier if it is a keyword in the target language; remove _ suffix when importing if the identifier is a keyword in the source language.

nomeata commented 4 years ago

At some point I think @rossberg suggested allowing quoted strings for field names. We could extend this to this problem, i.e. type "record" = nat.

Or maybe it is less pressing here because type aliases do not matter for compatibility (unlike field names), so maybe appending …_ is fine.

We may not even have to remove the suffix, as round-tripping of type names is not required. But we could, of course. No strong opinion yet.

rossberg commented 4 years ago

Doesn't seem worthwhile to go out of our way to allow arbitrary identifier names. If you wanted to map them back to other languages that would likely be trouble anyway. I think restricting to common identifier syntax and escaping with an underscore is fine.