Skip to content
This repository has been archived by the owner on Feb 7, 2019. It is now read-only.

Function defaults in @traitdef #29

Open
mindbound opened this issue Nov 26, 2015 · 2 comments
Open

Function defaults in @traitdef #29

mindbound opened this issue Nov 26, 2015 · 2 comments

Comments

@mindbound
Copy link

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
Copy link
Owner

mauro3 commented Nov 26, 2015

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
Copy link
Author

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants