# How can I use a Holy trait defined in a module outside of it?

**URL:** <https://discourse.julialang.org/t/how-can-i-use-a-holy-trait-defined-in-a-module-outside-of-it/116408>\
**Category:** General Usage\
**Tags:** module, traits\
**Created:** [June 30, 2024, 6:06am UTC](https://discourse.julialang.org/t/how-can-i-use-a-holy-trait-defined-in-a-module-outside-of-it/116408 "2024-06-30T06:06:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![paalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paalon/32/5784_2.png) [@paalon](https://discourse.julialang.org/u/paalon)\
**Post date:** [June 30, 2024, 6:06am UTC](https://discourse.julialang.org/t/how-can-i-use-a-holy-trait-defined-in-a-module-outside-of-it/116408/1 "2024-06-30T06:06:33Z")

</div>

I’m studying Julia’s Holy traits along with traits in Rust, Scala and the original traits paper, “[Traits: A Mechanism for Fine-grained Reuse](http://web.cecs.pdx.edu/~black/publications/TOPLASTraits.pdf)”. I want to use a trait `Color` defined in `ColorModule` outside of its module. I wrote the following code:

```julia
module ColorModule
    abstract type Color end
    struct Blue <: Color end
    printlncolor(x) = printlncolor(Color(x), x)
    printlncolor(::Blue, ::Any) = println("blue")
end

using .ColorModule

struct Kingfisher end
ColorModule.Color(::Kingfisher) = ColorModule.Blue() # is this line safe?

kingfisher = Kingfisher()
ColorModule.printlncolor(kingfisher)

```

Is this safe code, or am I missing something important? I’m worrying the line of the definition of `ColorModule.Color(::Kingfisher)`.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [June 30, 2024, 7:26am UTC](https://discourse.julialang.org/t/how-can-i-use-a-holy-trait-defined-in-a-module-outside-of-it/116408/2 "2024-06-30T07:26:11Z")

</div>

Technically, this is fine, especially if `ColorModule` is your own module.

Preferably, you would only use names within a module that were either `export`ed or declared [`public`](https://docs.julialang.org/en/v1.11.0-rc1/base/base/#public).

---

<div class="post-metadata">

**Author:** ![paalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paalon/32/5784_2.png) [@paalon](https://discourse.julialang.org/u/paalon)\
**Post date:** [June 30, 2024, 7:43am UTC](https://discourse.julialang.org/t/how-can-i-use-a-holy-trait-defined-in-a-module-outside-of-it/116408/3 "2024-06-30T07:43:10Z")

</div>

How to use exported names? The following can’t go well.

```julia
module ColorModule
    export Color, Blue, printlncolor
    abstract type Color end
    struct Blue <: Color end
    printlncolor(x) = printlncolor(Color(x), x)
    printlncolor(::Blue, ::Any) = println("blue")
end

using .ColorModule

struct Kingfisher end
Color(::Kingfisher) = Blue()

kingfisher = Kingfisher()
printlncolor(kingfisher) # ERROR: MethodError: no constructors have been defined for Main.ColorModule.Color

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [June 30, 2024, 7:49am UTC](https://discourse.julialang.org/t/how-can-i-use-a-holy-trait-defined-in-a-module-outside-of-it/116408/4 "2024-06-30T07:49:28Z")

</div>

> [@paalon](#):
>
> ```julia
> ColorModule.Color(::Kingfisher) = ColorModule.Blue() # is this line safe?
> 
> ```

You should keep that definition since you are extending it. Alternatively, you could do `import ColorModule: Color`.

`export` or `public` (as of Julia 1.11) can serve as indicators that the name is intended to be used outside of the module. Typically, you should not be reaching into modules to use things that are not declare for public use. This is independent of the use of qualified syntax.
