Its clear the latter call wasn’t fused into a single loop since twice the memory was allocated.
My best guess is this isn’t supported, and maybe never will be because the ambiguity of what to do if myplus has other side-effects. However, is there any way to make something similar to this work, to allow shorthand for some longer operation than myplus? It certainly seems within the realm of reason that the compiler could figure out in simple cases like this that its safe to fuse.
In this case, @pabloferz’s suggestion will fuse, but it wouldn’t work if your user-defined function didn’t happen to also do the scalar operation you want – or more generally, if you have an operation that cannot be expressed as a vectorization of any scalar operation (e.g. matmul, matrix inversion). In such cases, we probably will never be able to guarantee fusion, although a sufficiently clever compiler optimization could observe that fusion is possible and do it for you.
At a higher level, the new syntactic loop fusion support means that one should shift programming styles away from what’s traditionally been done in vectorized languages:
In vectorized languages, one wants to push vectorization as far “up” as possible: compose more complex operations out of simpler vectorized operations, and then let vectorization flow from the top down to individual, optimized vectorized operations.
In Julia since 0.5, one wants to do the opposite and push scalar operations as far up as possible: express complex operations in entirely scalar form for as long as you can (i.e. until you hit an inherently array operation like a matmul or matrix inversion or whatever), and then vectorize at the very end by calling functions using f.(args...) syntax.
So in this case, if myplus can be defined to only operate on scalars, then do that – don’t define it to work on vectors at all. Instead, if you need to call it on vectors, use the myplus.(a, b) form.