Skip to content

Instantly share code, notes, and snippets.

@oxc
Last active August 29, 2015 14:20
Show Gist options
  • Save oxc/70c22ff998f988a4f30d to your computer and use it in GitHub Desktop.
Save oxc/70c22ff998f988a4f30d to your computer and use it in GitHub Desktop.
Eclipse 4.4.2 does not recognize trait Interfaces on scala singleton objects in Java code
/**
* This class demonstrates that Eclipse 4.4.2 for some reason does not recognize
* that the Scala singleton object for the class Implementor does in fact
* implement the interface Interface<String>.
*
* Instead, it shows the error markers mentioned in the comments.
*
* However, the code compiles fine (by the Eclipse compiler!), and in fact
* running this class' main method prints:
*
* <pre>
* Hello World
* Hello World
* Hello World
* Hello World
* </pre>
*
*
*
*/
public class BugDemo {
public static void main(String[] args) {
testParameter();
testAssignment();
testCast();
testObjectCast();
}
/**
* "The method print(Interface<String>) in the type Caller is not applicable
* for the arguments (Implementor$)"
*/
private static void testParameter() {
print(Implementor.ref());
// ^^^^^
}
/**
* "Type mismatch: cannot convert from Implementor$ to Interface<String>"
*/
private static void testAssignment() {
Interface<String> foo = Implementor.ref();
// ^^^^^^^^^^^^^^^^^
print(foo);
}
/**
* "Cannot cast from Implementor$ to Interface<String>"
*/
private static void testCast() {
print((Interface<String>) Implementor.ref());
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
/**
* This one works, obviously.
*/
private static void testObjectCast() {
Object foo = Implementor.ref();
print((Interface<String>) foo);
}
private static void print(Interface<String> foo) {
System.out.println(foo.someMethod());
}
}
trait Interface[T] {
def someMethod: T
}
object Implementor extends scala.AnyRef with Interface[String] {
def someMethod: String = "Hello World"
def ref: this.type = this
}
/**
* To make things even stranger, if I add the following code to Implementor.scala,
* all the beforementioned errors become a Warning "Type safety: The expression of
* type Implementor$ needs unchecked conversion to conform to Interface<String>".
*
* This does NOT happen if I put it in a separate file (e.g. "Test.scala")
*/
object Test {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment