Actually I think it was an error by Wolfram to do this. They should have used another operator with the correct associativity, or maybe even invented a completely new one. Or even better, just used normal expressions with the object passed as first argument (i.e. method1[obj, args]). And if they wanted to have objects to the left, provide a general syntax (e.g. an extended infix syntax, transforming a~b~[...] into b[a,...], so you could write obj~method~[args], but also e.g. 5~IntegerDigits~[2,3]).
–
celtschkOct 11 '12 at 16:37

@celtschk That would be an interesting syntax. I can only imagine the resistance it would be met with however, as even simple ~infix~ has been a hard sell. :^)
–
Mr.Wizard♦Oct 11 '12 at 16:50

2 Answers
2

The problem is that the conversion happens at parsing stage, not evaluation. And after code has been parsed, the details of how function was invoked (prefix, normal way or postfix) are not stored any more, so by the time you evaluate the code, you have no way to tell whether you typed it as f@g@h, f[g[h]], or h // g // f.

Note that the above code may not be totally robust, since it is based on interpreting box expressions, and I may have missed some possibilities. It should be possible to make it reasonably robust though, by extending to more cases.

Since a single @, as far as I know, does not have any other meaning in Mathematica (other than a prefix function call), another option you have is to write a string preprocessor which will be called to preprocess your code before it is parsed by Mathematica parser, inserting parentheses as necessary. This method can probably be made to work for packages as well.

The general solution to this problem is to write a programmable preprocessor for Mathematica (for some languages like OCaml, such preprocessors exist), which would allow one to define an arbitrary syntax. If such a thing appears and becomes mature and robust, then code using it could be shared, I think. Until then, I tend to agree with you.
–
Leonid ShifrinOct 11 '12 at 17:11

That would be very useful. Let's try to talk to relevant people next week at the conference. I really think that metaprogramming is an extremely nice and unique feature of Mathematica and should be improved.
–
Rolf MertigOct 11 '12 at 17:59

@RolfMertig I have been thinking about implementing this for quite some time. Perhaps will move that up on my todo list. We can discuss during the conference.
–
Leonid ShifrinOct 11 '12 at 18:43

Mathematica is a registered trademark of Wolfram Research, Inc. While the mark is used herein with the limited permission of Wolfram Research, Stack Exchange and this site disclaim all affiliation therewith.