Question

Could someone explain me this error:

user> (let [^int i 3] i)
CompilerException java.lang.UnsupportedOperationException: Can't type hint a local with a primitive initializer, compiling:(NO_SOURCE_PATH:1)

I don't understand,

  • what exactly I cannot type-hint and why?

  • why I can use an array type hint in the same situation?

    user> (let [^ints ii (int-array 1)] ii)
    #<int[] [I@334a123f>
    
  • how to type-hint local integer variables?

Was it helpful?

Solution

This exception is thrown by the compiler from this line. Basically, if you use an expression which is a primitive constant or something that can be evaluated at compile time to be a primitive constant, like: (+ 1 10), the compiler can detect the type of the object itself and doesn't need type hinting. Check the getJavaClass and hasJavaClass methods in the same class in which the earlier link points to. These methods check if the expression is primitive then get the class from the expression itself, otherwise use type hinting if provided.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top