Scala:如何实现包含scala“typed”类而没有类型参数的java接口

时间:2022-11-25 19:35:12

This is a tricky one - I have a java interface that I want to implement in scala:

这是一个棘手的问题 - 我有一个我想在scala中实现的java接口:

public interface Foo {
  public void bar(scala.Array arr);
}

Is it even possible to implement in scala? when I try:

甚至可以在scala中实现吗?当我尝试:

class FooImpl extends Foo {
  override def bar(arr: Array[_]): Unit = ???
}

I get:

Error:(13, 7) class FooImpl needs to be abstract, since method bar 
in trait Foo of type (x$1: Array)Unit is not defined
(Note that Array does not match Array[_]. To implement a raw type, use 
Array[_])
class FooImpl extends Foo {

2 个解决方案

#1


2  

The error message is giving you the answer for any generic type other than Array (after replacing the name, of course):

错误消息为您提供除Array之外的任何泛型类型的答案(当然,在替换名称之后):

To implement a raw type, use Array[_]

要实现原始类型,请使用Array [_]

"Raw type" is what Java calls a generic type used without a type parameter and e.g. https://docs.oracle.com/javase/tutorial/java/generics/rawTypes.html explains why you should not use them except to interface with now horribly obsolete pre-Java-5 code. So if it is at all an option, you should fix the Java interface in the first place.

“原始类型”是Java在没有类型参数的情况下使用的泛型类型,例如https://docs.oracle.com/javase/tutorial/java/generics/rawTypes.html解释了为什么你不应该使用它们,除了与现在可怕的过时的Java-5前代码接口。因此,如果它是一个选项,您应该首先修复Java接口。

Now, why does this not work for Array? It's a special type, which is really built into compiler. Its instances are real JVM arrays, which don't have a common type in Java. So when it's used in Scala code, the compiled bytecode doesn't use scala.Array at all. I guess that it only exists as a JVM type (unlike e.g. scala.Any or scala.Null) to put the static methods there, but all instance methods are defined as throw new Error(). It seems the error message didn't take this unique case into account.

现在,为什么这对Array不起作用?它是一种特殊的类型,它真正内置于编译器中。它的实例是真正的JVM数组,它们在Java中没有通用类型。因此,当它在Scala代码中使用时,编译的字节码根本不使用scala.Array。我猜它只存在于JVM类型(不像scala.Any或scala.Null),以便将静态方法放在那里,但所有实例方法都定义为throw new Error()。似乎错误消息没有考虑这个独特的情况。

So, the answer is: no, it can't be implemented in Scala, as far as I am aware. But it can't be non-trivially implemented in Java either. And even for trivial implementations, you'd run into the same issues when trying to write code using it.

所以,答案是:不,据我所知,它不能在Scala中实现。但它也不能在Java中非常简单地实现。即使对于琐碎的实现,在尝试使用它编写代码时也会遇到同样的问题。

#2


0  

To make the code work you either have to

要使代码工作,您必须这样做

  • Make the FooImpl declaration as abstract class

    将FooImpl声明设为抽象类

  • Implement the bar method

    实现bar方法

because "Java interfaces don’t implement behaviour".

因为“Java接口不实现行为”。

For your reference see this page.

供您参考,请参阅此页面。

#1


2  

The error message is giving you the answer for any generic type other than Array (after replacing the name, of course):

错误消息为您提供除Array之外的任何泛型类型的答案(当然,在替换名称之后):

To implement a raw type, use Array[_]

要实现原始类型,请使用Array [_]

"Raw type" is what Java calls a generic type used without a type parameter and e.g. https://docs.oracle.com/javase/tutorial/java/generics/rawTypes.html explains why you should not use them except to interface with now horribly obsolete pre-Java-5 code. So if it is at all an option, you should fix the Java interface in the first place.

“原始类型”是Java在没有类型参数的情况下使用的泛型类型,例如https://docs.oracle.com/javase/tutorial/java/generics/rawTypes.html解释了为什么你不应该使用它们,除了与现在可怕的过时的Java-5前代码接口。因此,如果它是一个选项,您应该首先修复Java接口。

Now, why does this not work for Array? It's a special type, which is really built into compiler. Its instances are real JVM arrays, which don't have a common type in Java. So when it's used in Scala code, the compiled bytecode doesn't use scala.Array at all. I guess that it only exists as a JVM type (unlike e.g. scala.Any or scala.Null) to put the static methods there, but all instance methods are defined as throw new Error(). It seems the error message didn't take this unique case into account.

现在,为什么这对Array不起作用?它是一种特殊的类型,它真正内置于编译器中。它的实例是真正的JVM数组,它们在Java中没有通用类型。因此,当它在Scala代码中使用时,编译的字节码根本不使用scala.Array。我猜它只存在于JVM类型(不像scala.Any或scala.Null),以便将静态方法放在那里,但所有实例方法都定义为throw new Error()。似乎错误消息没有考虑这个独特的情况。

So, the answer is: no, it can't be implemented in Scala, as far as I am aware. But it can't be non-trivially implemented in Java either. And even for trivial implementations, you'd run into the same issues when trying to write code using it.

所以,答案是:不,据我所知,它不能在Scala中实现。但它也不能在Java中非常简单地实现。即使对于琐碎的实现,在尝试使用它编写代码时也会遇到同样的问题。

#2


0  

To make the code work you either have to

要使代码工作,您必须这样做

  • Make the FooImpl declaration as abstract class

    将FooImpl声明设为抽象类

  • Implement the bar method

    实现bar方法

because "Java interfaces don’t implement behaviour".

因为“Java接口不实现行为”。

For your reference see this page.

供您参考,请参阅此页面。