I’m utterly confused why the following code doesn’t produce identical results (i.e. selecting on type with @match (Match.jl) or if…elseif…end). With @match (get_fmt1) regardless of the actual type, always an integer type is matched…? More insight much appreciated.
using Match
function get_fmt1(a)
T = typeof(a)
S = supertype(T)
println("Ta, Sa = $T, $S")
fmt(a) = @match S begin
# Signed => "d"
Integer || Signed || Unsigned || Bool || BigInt => "d"
AbstractFloat => "f"
AbstractString => "s"
_,Any => "s"
end
fmt(a)
end
function get_fmt2(a)
T = typeof(a)
S = supertype(T)
# println("Ta, Sa = $T, $S")
if S in [Integer Signed Unsigned Bool BigInt ] return "d"
elseif S == AbstractFloat return "f"
elseif S == AbstractString return "s"
else return "o"
end
end
trials = [ 1 1.0 "Foo" Inf ]
println("FMT 1")
for a in trials
Ta = typeof(a)
Sa = supertype(Ta)
println("a, Ta, Sa = $a, $Ta, $Sa")
println("fmt a = ", get_fmt1(a))
end
println("FMT 2")
for a in trials
Ta = typeof(a)
Sa = supertype(Ta)
println("a, Ta, Sa = $a, $Ta, $Sa")
println("fmt a = ", get_fmt2(a))
end
@Tamas_Papp The “Julia proper way” would be to dispatch type_letter(S) on typeof(S) (…you notice I’m coming from the Python world…), the match behavior still seems problematic to me – I have posted an issue to the Match.jl github repos.