Traits.jl
Traits.jl copied to clipboard
Function defaults in @traitdef
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.
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.
Thanks for the reply, I'll be looking forward to this.