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

abstract types as traits #1

Open
hayd opened this issue Nov 1, 2014 · 5 comments
Open

abstract types as traits #1

hayd opened this issue Nov 1, 2014 · 5 comments

Comments

@hayd
Copy link

hayd commented Nov 1, 2014

I'm just throwing this out here from discussion in JuliaLang/julia#6975 (comment) (@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
Copy link
Author

hayd commented Nov 2, 2014

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

mauro3 commented Nov 2, 2014

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

hayd commented Nov 2, 2014

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

mauro3 commented Nov 2, 2014

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

mauro3 commented Apr 9, 2015

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.

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