mauro3 / Traits.jl

Exploration of traits in Julia
Other
39 stars 6 forks source link

Function defaults in @traitdef #29

Open mindbound opened 8 years ago

mindbound commented 8 years ago

Is it possible to define (not just declare) functions in @traitdef so that all @traitimpls would have that function defined implicitly (cf. e.g. https://github.com/jasonmorton/Typeclass.jl), and, if not, how hard would it be to implement this?

It would allow to avoid a great deal of repetition in cases where a large function has to be included in each @traitimpl, with only argument types being different.

mauro3 commented 8 years ago

Yep, I've thought about this but it's not implemented. You can fudge it though:

using Traits

@traitdef Tr{X} begin
    f(X)
    g(X)
end

# default
f(x) = "default"

@traitimpl Tr{Int} begin
    g(::Int) = 7
end

However, that means that f is defined for all types. It would be nice, as you say, to have a default defined for a implemented type. I hope to add this but cannot give you any time-frame.

mindbound commented 8 years ago

Thanks for the reply, I'll be looking forward to this.