Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't type hint a local with a primitive initializer

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?

like image 718
sastanin Avatar asked Mar 05 '13 17:03

sastanin


1 Answers

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.

like image 193
Ankur Avatar answered Oct 24 '22 12:10

Ankur