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

paalon
Sun, 30 Jun 2024 06:06:33 +0000
General Usage

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”. I want to use a trait Color defined in ColorModule outside of its module. I wrote the following code:

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

4 posts - 2 participants