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

Parametrized structs with traits

Open AriMKatz opened this issue 5 years ago • 3 comments

What do you think about a macro that allows defining a structs parametrized on traits instead of subtypes?

Can the compiler be made to specialize the same way as using subtypes?

AriMKatz avatar May 04 '20 10:05 AriMKatz

can you give an example, please?

schlichtanders avatar May 04 '20 15:05 schlichtanders

Sure.

We have this example from the manual:

struct Rational{T<:Integer} <: Real
    num::T
    den::T
end

Instead we can have:

@trait struct Rational{numberness(T)<:IntegerLike} <: Real
    num::T
    den::T
end

I don't have a specific use case for this at the moment, but it would be cool for traits to have broader utility.

Related: https://github.com/vtjnash/ComputedFieldTypes.jl and https://github.com/JuliaLang/julia/issues/18466

AriMKatz avatar May 07 '20 08:05 AriMKatz

Interesting case. I haven't yet thought about this and don't know the details how it is implemented in Julia.

But on a first sight, something like this should be possible, as in the end also the existing Rational struct just offers itself as a couple of functions with which you can construct that struct.

For sure there will be edge cases, but I can imagine that you can overwrite the constructer functions such that the checks are properly done.

Very interesting suggestion.

schlichtanders avatar May 07 '20 13:05 schlichtanders