Safety phase does not consider associated types when checking checked_cast
Am I doing something wrong or is this an error in the language?
import java.lang.Object
import java.lang.Integer
def main(): Unit =
let t: Integer = Box.box(0x02000000);
let _a: Object = checked_cast(t);
()
-- Safety Error -------------------------------------------------- T:\flix\test\src\Main.flix
>> Illegal checked cast: Attempt to cast a non-Java type to a Java type.
7 | let _a: Object = checked_cast(t);
^^^^^^^^^^^^^^^
illegal cast
From: Boxed[Int32]
To : java.lang.Object
What are you trying to do? What's the bigger program / real context?
Ideally you should not be using checked/unchecked cast.
~~Box[Int32] is not the same as Integer. You should use Integer.valueOf if you need to create a Java Integer for some reason.~~
Box[Int32]is not the same asInteger. You should useInteger.valueOfif you need to create a JavaIntegerfor some reason.
I am unsure actually. I think its an associated type which is actually Integer.
Box[Int32]is not the same asInteger. You should useInteger.valueOfif you need to create a JavaIntegerfor some reason.I am unsure actually. I think its an associated type which is actually Integer.
:O You're right!
@Bobini1 It seems there is a small bug in the compiler here. Nevertheless, casts should be rare, so it would be nice to know what the "real program" needs to do. Unless you are just experimenting?
@magnus-madsen I'm trying to call this method via Java interop: https://java-native-access.github.io/jna/4.2.0/com/sun/jna/Function.html#invoke-java.lang.Class-java.lang.Object:A-
In particular, I would like to try to use it to call __stdcall NtQueryDirectoryFile from ntdll.dll and CreateFileW from Kernel32.dll.
OK, you can use an unchecked cast for now: https://doc.flix.dev/unchecked-casts.html