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

**URL:** <https://discourse.julialang.org/t/define-new-method-in-package-what-is-the-syntax-for-importing-the-generic/90622>\
**Category:** New to Julia\
**Tags:** package, methods\
**Created:** [November 22, 2022, 3:18am UTC](https://discourse.julialang.org/t/define-new-method-in-package-what-is-the-syntax-for-importing-the-generic/90622 "2022-11-22T03:18:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![baptiste](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baptiste/32/9813_2.png) [@baptiste](https://discourse.julialang.org/u/baptiste)\
**Post date:** [November 22, 2022, 3:18am UTC](https://discourse.julialang.org/t/define-new-method-in-package-what-is-the-syntax-for-importing-the-generic/90622/1 "2022-11-22T03:18:59Z")

</div>

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

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

```

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

```julia
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

```julia
# 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.

---

<div class="post-metadata">

**Author:** ![magister-ludi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/magister-ludi/32/4003_2.png) [@magister-ludi](https://discourse.julialang.org/u/magister-ludi)\
**Post date:** [November 22, 2022, 5:23am UTC](https://discourse.julialang.org/t/define-new-method-in-package-what-is-the-syntax-for-importing-the-generic/90622/2 "2022-11-22T05:23:21Z")

</div>

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:

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

```

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

---

<div class="post-metadata">

**Author:** ![baptiste](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baptiste/32/9813_2.png) [@baptiste](https://discourse.julialang.org/u/baptiste)\
**Post date:** [November 22, 2022, 6:26am UTC](https://discourse.julialang.org/t/define-new-method-in-package-what-is-the-syntax-for-importing-the-generic/90622/3 "2022-11-22T06:26:41Z")

</div>

Sorry I don’t really understand what you mean. The original function defined in the `Rotations` package is [`RotZYZ(theta1, theta2, theta3)`](https://juliageometry.github.io/Rotations.jl/stable/functionreference/#Rotations.RotZYZ), 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.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [November 22, 2022, 6:53am UTC](https://discourse.julialang.org/t/define-new-method-in-package-what-is-the-syntax-for-importing-the-generic/90622/4 "2022-11-22T06:53:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![baptiste](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baptiste/32/9813_2.png) [@baptiste](https://discourse.julialang.org/u/baptiste)\
**Post date:** [November 22, 2022, 6:57am UTC](https://discourse.julialang.org/t/define-new-method-in-package-what-is-the-syntax-for-importing-the-generic/90622/5 "2022-11-22T06:57:24Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![magister-ludi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/magister-ludi/32/4003_2.png) [@magister-ludi](https://discourse.julialang.org/u/magister-ludi)\
**Post date:** [November 22, 2022, 7:00am UTC](https://discourse.julialang.org/t/define-new-method-in-package-what-is-the-syntax-for-importing-the-generic/90622/6 "2022-11-22T07:00:13Z")

</div>

The manual section on [modules](https://docs.julialang.org/en/v1/manual/modules/) is probably a good place to start.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [November 22, 2022, 7:37am UTC](https://discourse.julialang.org/t/define-new-method-in-package-what-is-the-syntax-for-importing-the-generic/90622/7 "2022-11-22T07:37:11Z")

</div>

It’s documented in this [part](https://docs.julialang.org/en/v1/manual/modules/#using-and-import-with-specific-identifiers,-and-adding-methods) of the modules section.

---

<div class="post-metadata">

**Author:** ![baptiste](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baptiste/32/9813_2.png) [@baptiste](https://discourse.julialang.org/u/baptiste)\
**Post date:** [November 23, 2022, 2:53am UTC](https://discourse.julialang.org/t/define-new-method-in-package-what-is-the-syntax-for-importing-the-generic/90622/8 "2022-11-23T02:53:36Z")

</div>

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:

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

```

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

The alternative also surprised me,

```julia
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.
