Define new method in package – what is the syntax for importing the generic?

I’m using various functions from Rotations in my own package, and at some point decided to create a new method for convenience,

# splatted version
RotZYZ(v::SVector) = RotZYZ(v...)

but I seem to get in trouble with this kind of error,

ERROR: MethodError: no method matching RotZYZ(::Float64, ::Float64, ::Float64)

which puzzled me greatly: even though Rotations is imported in my package, and I can call it in interactive use, it seems to be missing. Eventually I remembered I’d defined a new method, and figured out that if I redefine it more explicitly as

# splatted version
RotZYZ(v::SVector) = Rotations.RotZYZ(v...)

then all is well (I believe). But clearly there’s something I have not understood about the search path for existing methods, so I’m hoping someone could point me to some reference explaining the proper way to define new methods, import generics from other packages, etc. I come from R, where it’s apparently a little different.

Your new function is not in the same package as the original function (it’s probably in Main, but without more context, that’s hard to tell). You need to declare your new function so it’s in that same package as the original:

Rotations.RotZYZ(v::SVector) = RotZYZ(v...)

otherwise your new declaration hides any that’s in Rotations.

1 Like

Sorry I don’t really understand what you mean. The original function defined in the Rotations package is RotZYZ(theta1, theta2, theta3), and I’m wanting to define a new method in MyPackage for a vector argument; I find that I need to reference Rotations.RotZYZ(theta1, theta2, theta3) explicitly even though I import Rotations, so I’m a bit perplexed by the rules of defining new methods.

If you want to extend a function you either need to specify the module, Rotations.RotZYZ, or import that specific symbol, import Rotations: RotZYZ.


Ah, OK, thanks. Do you know where this might be documented? I had no luck searching with the keywords I could think of (too generic).

The manual section on modules is probably a good place to start.

1 Like

It’s documented in this part of the modules section.

1 Like

Thanks both, I now feel more qualified, so to speak. It’s a little unintuitive to me that one would use the original namespace to define a new method, as in:

NiceStuff.nice(::Cat) = "nice 😸"

but now I know that’s the syntax to use.

The alternative also surprised me,

nice(::Cat) = "nice 😸"

which is what I’d naively started with, but I was only using .NiceStuff in my module, where in fact I need to also (/alternatively) import .NiceStuff: nice to make it available unqualified.