I’m trying to write a function that enforces numeric types. The eventualy goal is to create a library to support the use of fixed-point.
So i tried the simple example of
function foo(y::UInt8)::UInt8 x::UInt8 = 10 return x+y end
julia> foo(250) ERROR: MethodError: no method matching foo(::Int64)
so to solve that problem I simply did:
julia> foo(convert(UInt8, 250)) 0x04
Naturally I would like foo(250) to work. So give that this is a multiple dispatch language I figure that i simply can do:
function foo(y) foo(convert(UInt8, y)) end
and that does in fact do what I want.
I thought this might actually be a good example for those of use new to Julia.
My question is : Is this the right approach ?
My other question is , if i define foo thusly
function foo(y::UInt8)::UInt8 x = 10 return x+y end
julia> foo(convert(UInt8, 250)) ERROR: InexactError()
I can see why there would be a problem since I’m sure x is defaulting to Int64, but that doesn’t seem like the right error. Or is it ? I expect that the conversion can’t be done without the possibility of overflow ?