Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia: How to avoid auto-promotion type changes?

Tags:

types

julia

For example:

n::Uint8 = 0x00
x::Uint8 = n + 0x10
ERROR: type: typeassert: expected Uint8, got Uint64

I assume this happens because methods(+) for a::Uint8, b::Uint8 is not defined so n is auto-promoted to Uint64. Is there a better way to deal with this than casting everything back to it's pre-promoted type after every operation? Isn't this something that the interpreter should be able to handle automatically (i.e. if it's told x should be assigned a Uint8 after the addition)?

like image 981
user3467349 Avatar asked Feb 09 '15 01:02

user3467349


1 Answers

I don't think there is a better way in Julia 0.3 than

julia> typeof(uint8(0x00 + 0x10))
UInt8

but in Julia 0.4 you don't need to worry as it doesn't do that automatic promotion anymore:

julia> typeof(0x00 + 0x10)
UInt8
like image 68
IainDunning Avatar answered Nov 15 '22 09:11

IainDunning