2

this is pretty much the code setup:

// java
public class JavaA { 

}

// scala
class ScalaA() extends JavaA {

}

but then in java, if i have an instance of ScalaA somewhere (or even if its null), but try to do this:

ScalaA scalaA = ...
if (scalaA instanceof JavaA) { ... }

on the line with instanceof, intellij doesn't show any error. but if i try to compile the code, i get the java: incompatible types

i've included the scala sdk as a project depedency, the java versions are correct, ive tried invalidating the cache/restarting, etc, but it doesnt seem to work. does anyone know how to fix this?

also, the scala class, it's compiled in a .jar file, and i imported it as a dependency. just like with the scala SDK and stuff

edit: i found a "solution" that seems to work; its not really a fix, but it works for now. i basically just used a function called "forceCast", and the code looks like this:

public static <T> T forceCast(Object value) {
    try {
        return (T) value;
    }
    catch (Exception e) {
        return null;
    }
}

and then

ScalaA scalaA = ...
JavaA javaA = forceCast(scalaA);

realistically, an exception should never be thrown in this case. this actually did fix my issue, and let me compile the code. im not sure about using instanceof, but i guess what i could do is just cast scalaA to an object, and do it that way

2
  • 1
    Could you give instructions how to go from an empty directory to actually seeing that specific error? That may dramatically increase the chances of someone discovering what's going wrong. Commented Jul 12, 2021 at 15:06
  • create a java project, and then the scala class i found the problem in, was from a minecraft mod "forge multipart". so i included that jar as a dependency allowing me to use the classes. the exact details: the scala class inherits "TileEntity" and is called "TIleMultipart". if i have a variable of the TIleMultipart and try to cast it to a TileEntity or the other way round, it shows no error. but when i try to compile it, it shows the incompatible types error Commented Jul 12, 2021 at 16:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.