Hi
Don’t panic, this is not a debate about whether infix operators are a good thing (I hope).
Background:
My old Reason code does this sort of thing quite a bit (QQ here is a thin wrapper around zarith’s Q.Int
rationals, and the %-
operator is subtraction of those rationals)
let pxHPos =
QQ.to_float(hPos %- Data.initialScheduleValue)
*. float_of_int(pixelsPerRepeat);
This code was written as a spike to experiment with some UI, I picked the first way to write it that seemed to work, and I expect I won’t use any of it in production code – though I will be wanting to use rationals and then converting them to float pixel values in CSS. So while I’m very much interested to hear how people think one should write arithmetic involving rationals and floats (maybe people here would advocate more strictly separating the two and just never having both in one expression, and defining and open
ing the standard arithmetic operators for use in rational expressions?), that’s not my focus in this post.
Of course the migration to ReScript syntax worked. However, it turns this for example (/%/
here is just Q.Int.of_ints and %+
is Q.Int.add):
QQ.to_float(i /%/ 1 %+ Data.initialScheduleValue)
|> Format.sprintf("%.2f"),
into:
QQ.to_float(\"%+"(\"/%/"(i, 1), Data.initialScheduleValue)) |> Format.sprintf("%.2f"),
Which does make it unreadable.
I’d like to use this code a bit more to play with my UI. Can I do that without rewriting it all? I heard that infix operators had gone, and then that they came back again precisely for migration purposes. However, I’ve not been able to figure out how to define infix operators in the new syntax. Any clues?
I’ve randomly tried things along these lines:
let \"%+" = QQ.\"%+"
let (\"%+") = QQ.\"%+"
let (%+) = QQ.\"%+"
But however I try to use them as operators (with or without the quoting), I get syntax errors.