Last active
December 25, 2015 08:29
-
-
Save freestrings/6947265 to your computer and use it in GitHub Desktop.
In Scala, arrays are not covariant.
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
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(); | |
} | |
} |
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
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