5

Seems one difference between Java Array and Scala Array is Java Array is co variant. Scala Array is not. Both are mutable. In java, sort method can take different arrays, such as arrays of String or Int. This is often quoted as a good example of Liskov substitution principle. Seems a good design to me? In Scala we know Array is not co variant. Although Scala Array is designed later than Java. I can't see how Scala Array is better in the aspect of co-variance. It has generic maybe this is better than Java.

2

2 Answers 2

11

When you look further, you will find that the fathers of the Java language later decided to make Generics not covariant. And that is for good reasons.

Because a List<Apple> is not a List<Fruit>. Otherwise, you could do things like

List<Apple> apples = new ArrayList<>();
List<Fruit> fruits = apples;
fruits.add(new Banana());

but wouldnt you expect that

Apple badApple = apples.get(0);

is a safe call? If you allow for covariance here, it is not!

And surprise: that is the problem that Java arrays have!

Therefore it is really a problem that Java arrays are covariant! And it is better that Scala arrays actually allow you to exactly control their "variance". In Scala, you have full control over variance; whereas in Java, they are always covariant.

Having covariant arrays was seen as pragmatic way to allow for "generic" methods like sort(Object[] objects) long before anybody thought of having generics in Java. That is almost 20 years in the past. The "modern" way is, as you can see: doing things differently.

The "cost" of this decision is the fact that Java also knows ArrayStoreException. That would not be necessary, if Java arrays would not be covariant!

Sign up to request clarification or add additional context in comments.

6 Comments

It's also worth noting that Scala generics are invariant by default, but can be declared as covariant or contravariant if needed - for example, scala.collection.Iterrable is covariant (and Scala collections in general tend to be covariant - I don't know if there are exceptions, other than arrays?). The safety of the variance is checked by the compiler.
Question was asking java Array but your example was List.
Lets step back: you asked "what's wrong with covariant arrays". I explained to you the actual problem that covariant "collections" have. In other words: "look, lists don't have this problem X here". Do you really need me to tell you "and that means arrays very well have that problem X"?! Together with that comment from @Cyäegha you really got all the information you need! But well, it seems so; thus I updated my answer.
Calm down. Bro. I am not saying your answer is wrong. Just saying your example could use Array rather than List. In Java Generics are invariant. which would make a different point.
Sorry, didn't want to come over as rude. I was just kinda surprised by your comment. Thanks for the accept!
|
0

scala arrays are mutable. the array values can be changed to a new value of the same type. you can not add or remove elements from the scala array without creating a new variable.

val values: Array[Int] = new ArrayInt

or

val values = new ArrayInt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.