Skip to content

Instantly share code, notes, and snippets.

@freestrings
Last active December 25, 2015 08:29
Show Gist options
  • Save freestrings/6947265 to your computer and use it in GitHub Desktop.
Save freestrings/6947265 to your computer and use it in GitHub Desktop.
In Scala, arrays are not covariant.
package freestrings.playground
public class ConvariantInjava {
class A {
}
class B extends A {
}
{
B[] b = new B[] { new B() };
A[] a = b;
a[0] = new A(); // ==> runtime error ( java.lang.ArrayStoreException )
};
public static void main(String[] args) {
new ConvariantInjava();
}
}
package freestrings.playground
object CovariantInScala {
class A {}
class B extends A {}
val b: Array[B] = Array(new B())
val a: Array[A] = b // ==> compile error
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment