Created
April 28, 2012 19:48
-
-
Save kijuky/2521622 to your computer and use it in GitHub Desktop.
Java側で定義したパラメータ型の可変長引数を持つメソッドは、Scala側でオーバーライドできない
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public abstract class AC<T> { | |
| protected abstract void cannot_override(T... xs); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class C extends AC[String] { | |
| override protected def cannot_override(args: String*) { | |
| for (str <- args) println(str) | |
| } | |
| } | |
| object Test extends App { | |
| new C().cannot_override("Hello", "world") | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| > javac -version | |
| javac 1.6.0_31 | |
| > scalac -version | |
| Scala compiler version 2.9.2 -- Copyright 2002-2011, LAMP/EPFL | |
| > scala -version | |
| Scala code runner version 2.9.2 -- Copyright 2002-2011, LAMP/EPFL | |
| > javac AC.java | |
| > scalac C.scala | |
| > scala Test | |
| java.lang.AbstractMethodError: C.cannot_override([Ljava/lang/Object;)V | |
| at Test$delayedInit$body.apply(C.scala:8) | |
| at scala.Function0$class.apply$mcV$sp(Function0.scala:34) | |
| at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12) | |
| at scala.App$$anonfun$main$1.apply(App.scala:60) | |
| at scala.App$$anonfun$main$1.apply(App.scala:60) | |
| at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59) | |
| at scala.collection.immutable.List.foreach(List.scala:76) | |
| at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:30) | |
| at scala.App$class.main(App.scala:60) | |
| at Test$.main(C.scala:7) | |
| at Test.main(C.scala) | |
| at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | |
| at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) | |
| at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) | |
| at java.lang.reflect.Method.invoke(Method.java:597) | |
| at scala.tools.nsc.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClassLoader.scala:78) | |
| at scala.tools.nsc.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:24) | |
| at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:88) | |
| at scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:78) | |
| at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:101) | |
| at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:33) | |
| at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:40) | |
| at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:56) | |
| at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:80) | |
| at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89) | |
| at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala) |
Author
JavaとScalaではprotectedの可視性が違うのですね・・・迂闊でした。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
objectの名前をclassと同じCにする(コンパニオンオブジェクトにする)と実行できますね。
もしくは、オーバーライドした際にprotectedを外す(publicにする)と実行できます。
ScalaではJavaと違ってprotectedなメソッドはインスタンス外部から呼べない(Javaは同一パッケージ内なら呼べるし、Scalaでもコンパニオンオブジェクトなら呼べる)ので、その辺りの問題だと思います。
しかしオーバーライドしていない通常のprotectedメソッドを呼ぼうとするとコンパイルエラーになるので、オーバーライドしたprotected抽象メソッドがコンパイルエラーにならないのは不思議なところです。