mauro3 / Traits.jl

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

abstract types as traits #1

Open hayd opened 9 years ago

hayd commented 9 years ago

I'm just throwing this out here from discussion in https://github.com/JuliaLang/julia/issues/6975#issuecomment-61313188 (@skariel).

Could a trait perform the roles of abstract types (giving the user the simplicity/familiarity of abstract inheritance whilst actually providing a trait), so we would write something like:

@traittype abstract Number
@traittype abstract Real     <: Number
@traittype abstract Integer  <: Real
@traittype abstract Signed   <: Integer
@traittype bitstype 64 Int64    <: Signed

(or maybe you'd just reuse @traidef.)

I think that abstract types are in some sense an example of traits, but please forgive my ignorance. (There's also the issue of defining functions function f(x::Number) ... end... and probably more...)

hayd commented 9 years ago

cc @abeschneider (who previously did type traits... perhaps that could be re-written using Traits.jl ?

also see @jiahao's post https://groups.google.com/forum/#!topic/julia-dev/iBBSQt1FGHE

mauro3 commented 9 years ago

Both abstract and concrete types are a bit like traits as they impliclty have an interface defined by the generic functions which apply to them. Concrete types, however, are also about storing data.

At the moment this would be a Traits replacement for Number:

@traitdef TNumber{N} begin
    @constraints begin
        N==Number
    end
end

But I have no idea whether traits should replace abstract types. And it would proably be hard as much of Julia is based on the current type system. And if so, a smart transition scheme should be put in place, which is what you're suggesting above.

hayd commented 9 years ago

It kind of feels like your awesome Group example is a way in for abstract base classes (e.g. they must have certain attributes). I don't quite follow the TNumber example above as it still references Number. Will think on this a bit more as I understand Traits a bit better!

Would be interesting to see if there is something here (as a concept), even thinking about being in base is way off IMO.

The idea would be it's traits all the way down...

mauro3 commented 9 years ago

Yes, the TNumber example is a bit ciruclar... It should work the same though if instead you include all the interface functions of Number into the trait-defintion.

And then continue on do the same for all the subtypes (sticking to single trait-inheritance) should lead to something fairly equvalent to what Julia has now, at least in principle.

mauro3 commented 9 years ago

Here some ideas on this https://github.com/mauro3/Traits.jl/blob/master/dev_notes.md#merging-implicit-interfaces-specified-on-types-with-traits. Although it's pretty rough, so you might not be able to make sense of it.