CanonicalTraits.jl
CanonicalTraits.jl copied to clipboard
Reasoning about return types?
Hi Taine,
In one of your example, you have
@trait Addable{L, R} begin
(+) :: [L, R] => Any
(+) = Base.:+
end
I'd like to do something like this, but avoid the Any. I was thinking something like
@trait Addable{L, R, X} where {X = inferAddable(L,R)} begin
(+) :: [L, R] => X
(+) = Base.:+
end
But then I'm having trouble getting the @implement to work out. Is this currently possible?
Hi, Chad, this is possible, and you solution looks good!
I think there's no real reason to put the return type in trait parameters is there? You could just do
julia> @trait Addable{L, R} begin
(+) :: [L, R] => infer(+, L, R)
(+) = Base.:+
end
julia> infer(::typeof(+), ::Type{Int}, ::Type{Int}) = Int;
julia> @implement Addable{Int, Int}
julia> 1 + 1
2
If we chose the wrong return type, this fails correctly:
julia> infer(::typeof(+), ::Type{Int}, ::Type{Int}) = Float64;
julia> 1 + 1
ERROR: TypeError: in typeassert, expected Float64, got a value of type Int64
Stacktrace:
Great point @MasonProtter , thanks!
This looks pretty good to me. I'm now thinking about how to make it version compatible.
Besides, @cscherrer 's proposal is already implemented.
It doesn't seem to work for me. Perhaps I'm doing something wrong?
julia> using CanonicalTraits
julia> inferAddable(::Type{Int}, ::Type{Int}) = Int
inferAddable (generic function with 1 method)
julia> @trait Addable{L, R, X} where {X = inferAddable(L,R)} begin
(+) :: [L, R] => X
(+) = Base.:+
end
julia> @implement Addable{Int, Int}
julia> 1 + 1
ERROR: Not implemented trait Addable for (Int64, Int64, Int64).
Stacktrace:
[1] error(s::String)
@ Base ./error.jl:33
[2] Addable(::Type, ::Vararg{Type, N} where N)
@ CanonicalTraits ~/.julia/dev/CanonicalTraits/src/Typeclasses.jl:157
[3] +(+#2451::Int64, +#2452::Int64)
@ Main ./none:0
[4] top-level scope
@ REPL[5]:1
@MasonProtter This code works:
@implement Addable{Int, Int, T} where T
Maybe I should enable something like
@implement Addable{Int, Int, _}
Directly supporting Addable{Int, Int} suffers from some analysis issues, but it is still negotiable.
Ah, I see! Thanks for explaining.