Yea, tried it. Like I said, I suppose it could do, but it is not properly extending the function and it cannot be imported en masse for a large number of functions without having to evaluate many more assignments.
module Foo
export +
+(a,b) = Base.:+(a,b)
+(a::Symbol, b::Symbol) = 2
end
julia> importall Foo
WARNING: ignoring conflicting import of Foo.+ into Main
Instead, the user has to be asked to do + = Foo.:+ for every single function. Theoretically, one could create a function that generates the code to do all those assignments for you, but the user would still have to enter something like eval(Foo.generated_assignments()), and evaluated in the Main scope.
julia> module Foo
export +
+(a,b) = Base.:+(a,b)
+(a::Symbol, b::Symbol) = 2
generated_assignments() = :(+ = Foo.:+)
end
Foo
julia> :x+:y
ERROR: MethodError: no method matching +(::Symbol, ::Symbol)
julia> eval(Foo.generated_assignments())
WARNING: imported binding for + overwritten in module Main
+ (generic function with 2 methods)
julia> :x+:y
2
It would be preferable if the language itself could handle this properly.
Specifically, instead of ignoring conflicting imports, it could replace them instead. Then this would change how everything works slightly, but would be more flexible perhaps. Or a new keyword should exist so that the imports can be forced to overwrite the existing function, like local import Foo, but with the same effect as having eval(Foo.generated_assignments()). That should’nt be too difficult to add to Julia, right?
People said it was impossible, but here we are. Looks like a local import can happen, it only needs to be part of the language now.