Traits.jl icon indicating copy to clipboard operation
Traits.jl copied to clipboard

Function defaults in @traitdef

Open mindbound opened this issue 9 years ago • 2 comments

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.

mindbound avatar Nov 26 '15 11:11 mindbound

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.

mauro3 avatar Nov 26 '15 12:11 mauro3

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

mindbound avatar Nov 26 '15 13:11 mindbound