Is there a reason to prefer one of the following two options? Option 1:
Base.:-(a::MyType) = MyType(-a.data)
Option 2:
import Base.-
-(a::MyType) = MyType(-a.data)
Is there a reason to prefer one of the following two options? Option 1:
Base.:-(a::MyType) = MyType(-a.data)
Option 2:
import Base.-
-(a::MyType) = MyType(-a.data)
Ultimately, I think it comes down to your specific context and coding preferences.
1: Maybe If you’re only extending Base.:-(...)
and not calling Base.-
elsewhere, this avoids unnecessary imports.
2: Maybe If you’re overloading multiple operators, using import Base.op
for each operator at the top can improve readability.
I prefer writing the module when adding a method because it’s easier to grep.
Second, @savq. You’re also less likely to accidentally pirate methods by using a name that you forgot you had imported.
Agreed, but I make an exception for operators. Base.:-(a::MyType)
is an eyesore, and besides I would never define my own version of -
distinct from Base. Are there any packages that do that? Sounds like a nightmare!
Any package which defines custom number-like objects, such as Symbolics.jl and Mods.jl.
Or accidentally define a new function when the symbol is no longer imported (accidental deletion of import statement, package’s export list changes and affects the draft using
statements). For the same reasons, reassigning an imported name outright errors without a properly qualified module. That makes it the primary purpose for me.
Nope.
julia> parentmodule(Symbolics.:-)
Base