2013-01-15 48 views
4

我已經在Java中定義的方法,如:如何覆蓋Scala中的Java可變參數方法?

void foo(int x, Thing... things) 

我需要重寫,在Scala中,但是這兩種給出錯誤:

override def foo(x: Int, things: Thing*) 
override def foo(x: Int, things: Array[Thing]) 

的錯誤是指<repeated...>但我不知道那是什麼。

更新

雪地...請不要介意。我在2.10.0,我錯誤鍵入了一些東西,沒有方法體。然後我對這個錯誤信息感到困惑,這對我來說似乎仍然很奇怪。在SBT:

> compile 
[info] Compiling 1 Scala source to [...]/target/scala-2.10/classes... 
[error] [...]/src/main/scala/Translator.scala:41: class MyMethodVisitor needs to be abstract, since method visitTableSwitchInsn is not defined 
[error] (Note that org.objectweb.asm.Label* does not match <repeated...>[org.objectweb.asm.Label]) 
[error] class MyMethodVisitor extends MethodVisitor (Opcodes.ASM4) { 
[error]  ^

的問題是,我只是visitTableSwitchInsn缺乏身體,但錯誤表明問題是可變參數參數的類型。

+1

這看起來像你可能會看到[錯誤](https://issues.scala-lang.org/browse/SI-4729)在2.10中修復。如果你認爲不行,你可以給你的版本號和更多的上下文嗎? –

+0

謝謝。我在我的大腦中看到了一個錯誤,再加上一個令人困惑的錯誤信息(我已經添加到該問題中)。 –

回答

1

的Java:

package rrs.scribble; 

public 
class VA1 
{ 
    public int va1(int... ints) { 
     return ints.length; 
    } 
} 

斯卡拉:

package rrs.scribble 

class VA1S 
extends VA1 
{ 
    override 
    def va1(ints: Int*): Int = 
    ints.length * 2 
} 

SBT:

> ~compile 
[info] Compiling 1 Scala source and 1 Java source to …/scribble/target/scala-2.10/classes... 
[success] Total time: 4 s, completed Jan 15, 2013 3:48:14 PM 
1. Waiting for source changes... (press enter to interrupt) 

這是斯卡拉2.10,這與@ TravisBrown的評論一致。

+0

謝謝。當我休息後回來時,我看到了明顯的代碼錯誤。 :|我的方法簡直缺乏一個機構。也許錯誤信息可能會更好。我更新了這個問題。 –