Search code examples
juliasplat

How to annotate type using the splat operator


How (is it possible) to annotate the type of arguments when using the splat operator?

f(x, y) = x^2 + y^2
vec = [1.0, 2.0, 'a']
f(vec[1:2]...)

How can I annotate that use of ... in the function call. Also notice that none of the macros to view code (@code_llvm, @code_lowered, @code_native, @code_typed, @code_warntype) work, so it would be very hard to optimize when using the splat?


Solution

  • Because it seems that in the above use-case, macro versions of reflection functions couldn't reach the right argument types, using original function instead of macro, could be helpful:

    f(x, y) = x^2 + y^2
    vec = [1.0, 2.0, 'a']
    @code_warntype(f(vec[1:2]...)) # => Nothing
    code_warntype(f,map(typeof,vec[1:2])) 
    #  Variables:
    #  x::Float64
    #  y::Float64
    #  .....
    

    This logic is true for all reflection macros, using their variant function with a (function, collection of types).

    references:

    1. The macro @code_warntype has function variant: @code_warntype
    2. How macros generated: macro generator
    3. Util function to reach types: gen_call_with_extracted_types