mauro3 / Traits.jl

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

Is it intended that traits cannot be satisfied by union types? #33

Open pkalikman opened 8 years ago

pkalikman commented 8 years ago

E.g.:

using Traits

@traitdef SomeTrait{X} begin
    some_function(X) -> Int64
end

immutable TypeOne
    x::Float64
end

immutable TypeTwo
    x::Int64
end

typealias FloatOrInt Union{TypeOne,TypeTwo}

some_function(x::FloatOrInt) =  1

(edited by @mauro3)

istrait(SomeTrait{FloatOrInt})
ERROR: Traits.TraitException("Trait SomeTrait{Union{TypeOne,TypeTwo}} has no type parameters.")

I traced this in the source to hasparameters, which seems it rejects this type because the union type is not a DataType.

Is there a principled reason for that? Offhand I don't see why this trait shouldn't be satisfied by the example.

Thanks

mauro3 commented 8 years ago

Yes, I think this should work but is not implemented at the moment. If a trait is fulfilled for all types of a union then the union should be part of the trait too.

mauro3 commented 8 years ago

I not gonna have time to work on this for a few weeks. If you want to have a crack at it, probably need to insert a loop at https://github.com/mauro3/Traits.jl/blob/cd4263735c23f864c7a75d740f5c6c29c0d2afff/src/Traits.jl#L143 which loops over all unions and calls istrait recursively.