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

(++) can't be used as an operator since it turns into (^) #2309

Open
leostera opened this issue Jan 2, 2019 · 5 comments
Open

(++) can't be used as an operator since it turns into (^) #2309

leostera opened this issue Jan 2, 2019 · 5 comments

Comments

@leostera
Copy link

leostera commented Jan 2, 2019

Problem:

The following OCaml input to refmt does not translate back to the same OCaml.

OCaml Input:

let (++) a b = a + b

Reason Output / Input:

let (++) = (a, b) => a + b;

OCaml Output:

let (^) a b = a + b

This means that libraries like notty can not be used, because they only define a (++) operator and no alternative function with a name.

Proposed solution:

Perhaps preserving all operators is impossible at this stage, but it'd be great if I didn't have to redefine operators because the OCaml output is different than what I'm expecting.

Alternatively, providing a cheatsheet of what operators will be replaced by what (and a way to circumvent it) would be a very nice gesture.

E.g,

Desc. Reason OCaml
String concatenation. (++) (^)
Value dereferencing. (^) (!)
Negation (!) not
Some custom operator ??? (++)
@jordwalke
Copy link
Member

In this case they are swapped, so you can use Notty with ^:

echo 'let (++) = (a, b) => 0;' | refmt --print ml

Outputs:

let (^) a b = 0

If you like ++ better for Notty, I suggest doing let (++) = (^); in your Reason file.

@leostera
Copy link
Author

leostera commented Jan 4, 2019

I don't mind which operator symbol it uses as long as I can use it. In this case, your suggestion does not seem to work:

echo "let (++) = (^);" | refmt --print ml
let (^) = (!)

Happy to put some time into this if you can point me in the right direction.

@jordwalke
Copy link
Member

You're right and you can see here which tokens are swapped for which tokens:
https://github.com/facebook/reason/blob/dcd42d7b071fa0ae302c1ba959590596fc466ee1/src/reason-parser/reason_syntax_util.ml#L89

@jordwalke
Copy link
Member

I think what we could do is allow prefixing the ++ and many like it with a backslash (or suffixing it with a backslash if that avoids more conflicts) to represent "The identifier should be this literal text in the AST".

That would always give you a way to refer to any ML function identifier like:

let (++) = (++\);

Then you could use the same ++ token in Reason files.

Sometimes when formatting an identifier with a trailing \, it might print it if there is a better Reason representation. Sometimes it won't.

@anmonteiro
Copy link
Member

#1944 should fix this when I get around to incorporating the suggested changes.

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

3 participants