I think not. Whatever works for functions should work for all callables, and some types can be callables and at the same time define methods for ^ (eg as an operator on some algebra).
The basic issue is that ^ in Julia is for iteration of *. So it would be inconsistent (and poorly composable as @Tamas_Papp notes) to overload it for iteration of ∘.
Thanks, my original post actually included a working implementation, but it’s nice to see another one using recursion. It makes me realize that my version only works for n>=2.
The difference is that your original version commits type piracy if it’s not in Base because it applies to all Function. That’s (one of) the reason(s) why you were suggesting its inclusion, I thought.
Mine applies only to the function that I own. That’s also the reason why I created a new function (with the same name) in the first place.
Does that make sense? I should have made the distinction clearer.
Implementation and syntax is obviously up for debate: For example, closure could work too e.g. 3 ^(+) 3, but I couldn’t figure out how to do it; I don’t know how to return an anonymous function that behaves like an operator.
I feel like a more general exponentiation operator has several benefits:
Convenient and intuitive iteration of any closed binary operator (e.g. the main post) without having to implement a new ^ method for custom types
When you have more than one product-like operation defined, you can eliminate ambiguity as to which operation your exponentiation is referring to.
If you want to be more strict, instead of overloading ^ for custom types to mean iteration of some non-* operator, you can instead specify exactly what you mean without much loss in convenience.
Code golf
As for how useful it would be in practice…I’m not the person to ask.
My dream is for regular exponentiation x^p to just be a special case via
I don’t think this is a very common use case for the typical Julia user.
It may be relevant algebra and similar, but it’s perfectly fine to have a package define some syntax for this (without type piracy, of course), perhaps using one of the available operators.
Interesting generalization… It’s basically providing a shorter reduce syntax for the case where the collection is a repetition of the same element. Then I would rather provide nice syntax for the collection itself: