2 ms·
Because function calls all are prefix and all use exactly the same way to indicate nesting (parentheses) and argument separation. I am in the camp that wants t
by Someone 12y ago
Because function calls all are prefix and all use exactly the same way to indicate nesting (parentheses) and argument separation.
I am in the camp that wants the power to define operators on new types, but I am not that sure about new operators.
For example, if Swift's implementation matches its documentation, one could make /"\\d{3,6}"/ a way to specify a regex:
- define a prefix operator /
- define a suffix operator /
- define prefix /(string s) to return some builder object that remembers that string
- define suffix /(builder b) to take the string that the builder remembered and produce a regex from it.
With appropriate precedence, that makes
/"\\d{3,6}"/
parse as
- Take the string "\\d{3,6}"
- Send it to the prefix operator /
- Send what comes out to suffix operator /
Unfortunately, that's all done at run time (or at least, users cannot enforce that the compiler does it at compile time), and of course, it would work with any string, for example:
/"\\d{3,6}" + myStringFunction("test")/
(Again, with good choice for operator precedence)
Ugly or genius? That is all in the eye of the beholder.
I am sure that techniques like this can earn you high marks in obfuscated Swift competitions, however (tip for beginners: one can define prefix operators // and /* that allow one to hide code in what look like comments)