On type annotations

I’m prototyping a macro to get rid of the conversions here:

julia> @noconvert function foo(x::Number)
           let x::Int = x
               x
           end
       end
foo (generic function with 2 methods)

julia> foo(3)
3

julia> foo(3.0)
ERROR: TypeError: in typeassert, expected Int64, got a value of type Float64

julia> foo(3.5)
ERROR: TypeError: in typeassert, expected Int64, got a value of type Float64

The code generation looks pretty clean:

julia> @code_llvm foo(3)
;  @ none within `foo`
define i64 @julia_foo_1572(i64 signext %0) #0 {
top:
  ret i64 %0
}

julia> @code_llvm foo(3.0)
;  @ none within `foo`
; Function Attrs: noreturn
define void @julia_foo_1574(double %0) #0 {
top:
  call void @j_overdub_1576(double %0) #6
  unreachable
}
2 Likes