Parametrized structs with traits
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?
can you give an example, please?
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
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.