reasonml / reason

Simple, fast & type safe code that leverages the JavaScript & OCaml ecosystems
http://reasonml.github.io
MIT License
10.09k stars 424 forks source link

[ideas] disable user level operators #1255

Closed bobzhang closed 2 weeks ago

bobzhang commented 7 years ago

follow up the bikeshedding on discord, it would be nice that we provide several operators but do it really well (formatting, precedence), |> , <| , arithmetic.

Bonus: we can also solve the jsx issue easily ><

chenglou commented 7 years ago

You mean no custom infix? What would we do to translate existing ocaml codebases over?

jordwalke commented 7 years ago

@chenglou You can always parse, print, and use infix operators like this:

let result = ( +++ ) a b;

If you wanted to be super opinionated, you could just create a white-list for which operators should have the privilege of being printed "in between".

Do we see people going overboard with infix operators?

chenglou commented 7 years ago

@jordwalke potentially with the monadic ones, which we can tastefully whitelist, if needed. I'm down with this approach btw. It'll also free us from ocaml's infix precedence rule. We could just say "we have 10 operators, their order is predetermined".

bassjacob commented 7 years ago

No custom infix would bind my hands a little. I know we want to avoid the haskell problem of nameless weird symbols being the public interface, but they turn let liftA4 f a b c d => A.apply (A.apply (A.apply (A.fmap f a) b) c) d; into let liftA4 f a b c d => f <$> a <@> b <@> c <@> d;. They can make a pretty big improvement to readability.

bassjacob commented 7 years ago

as an aside, if we replaced operators with the ability to call functions from an infix position (I'm sure this is incredibly hard) that would solve both problems too. the above could be comfortable written as:

let liftA4 f a b c d = f `fmap` a `ap` b `ap` c `ap` d;

Is it possible to use ppx or camlp4 to rewrite

a `f` b
as
(f a b)

?