Overload call to do multiplication on numbers?


I really like the 2x syntax, and get tired of writing stars.

edit: actual example…

function (x::Float64)(y)
a = 2.0

That looks like a hilariously bad idea, but I can’t find any actual problems with this. Anything I missed?


This already works without that definition though:

julia> 2.0pi

julia> 2.0(2+2)

julia> 2.0sin(pi)


Sorry about that, I changed my example but didn’t test :slight_smile: What didn’t work without the definition was:

a = 2.0

julia> pi(2+2)
ERROR: MethodError: objects of type Irrational{:π} are not callable

julia> a = 2

julia> a(2+2)
ERROR: MethodError: objects of type Int64 are not callable

But I get what you mean.

It looks like function call syntax, so for the examples above I would expect to call a function pi and a respectively.


Sorry again, I edited my second post (I should really wait to be on a computer before replying…)

My motivating example was writing sin(2pi(x+y)) by accident and wondering if that could work.


It’s definitely doable, but it seems a bit dangerous. I wouldn’t want to have sin = 666 somewhere and then write sin(x + y) later, thinking that I’m calling the sin function but actually just be multiplying x + y by the number of the beast.


I really like that in Mathematica a b Lowers to a*b. This would probably be horribly invasive and dangerous to implement in Julia but man would I love to be able to write sin(2pi (x+y)) and have the space between 2pi and (x+y) be understood as multiplication.


That’s fair enough, this feature, while very cool, is also fairly dangerous. For those interested:

function build_subtypes(T)
    concretes = [t for t in subtypes(T) if isempty(subtypes(t))]
    abstracts = [t for t in subtypes(T) if !isempty(subtypes(t))]
    types = []
    append!(types, concretes)
    for t in abstracts
        append!(types, build_subtypes(t))

for T in build_subtypes(Number)
    @eval begin
        function (x::$T)(y)


Space is already used as a separator in matrices, so sin.([2pi (x+y)]) would be ambiguous…


Right, I had a feeling there was something that would straight up clash with spaces as multiplication. How could I forget row vectors and matrices!