How to parameterize an argument which is either Int64,Int32, etc

I tried this and it stubbornly refuses to accept anything but Int64:

function testint(num::Int)
    print(num)
end
check = Int8(4)

testint(check)

ERROR: MethodError: no method matching testint(::Int8)
Closest candidates are:
  testint(::Int64)

Obviously “arg::Int” is implicitly becoming “arg::Int64”.

Ok this works:

function testint(num::Integer)
    print(num)
end
1 Like

Int === Int64 on 64 bit builds of Julia, while on 32-bit builds Int === Int32. That is, Int is concrete, aliasing the signed integer matching the Julia build type.
UInt is the same way, but for unsigned integers.

1 Like

Looks like you found the answer yourself. Just in case this is useful next time, if you are trying to find an appropriate supertype, use supertypes. For example:

julia> supertypes(Int)
(Int64, Signed, Integer, Real, Number, Any)

This allows you to choose the most appropriate supertype for your function signature. Hope that helps!

5 Likes