News for hackers. Stuff that matters.
Re:The Unsound Playground: Java and Scala's Type Systems are... by MrBuddyCasino on Sunday November 27, 2016 @04:12PM (#13051560)
Is this a defect or a tradeoff? Passing arrays of subtypes seems useful to me. I never experienced such an error, but I suspect this is quickly caught and shouldn't be a big problem.

But then I don't know anything about type system theory.

Re:The Unsound Playground: Java and Scala's Type Systems are... by Groxx on Sunday November 27, 2016 @05:34PM (#13052019)
Without generics (this is java 1, remember) it seems like a reasonable tradeoff. Otherwise[1] you can't have array-modifying polymorphic code at all - how would you write a "reverse this array" function, for instance? You'd need one for every kind of array you want to reverse. Or a "copy" function - even if you're given a properly-sized-and-typed array to put the values in, you can't make a function that'd work on anything except a single type.

You could force every array-function-caller to cast their args to e.g. `Object[]`, but that seems like it'd require more work for even less safety. Though I'd love a read-only concept in Java, for read purposes :|

[1]: I think. If I'm wrong, someone please correct me!

Re:The Unsound Playground: Java and Scala's Type Systems are... by RHSeeger on Sunday November 27, 2016 @08:40PM (#13052841)
You could define what an array accepts vs returns separately (input vs output). Admittedly, you're pretty much into generics (good ones, not what Java has) at that point.

Edit: Could use a better word for "good" that sounds less dismissive, but I couldn't think of one. Java's generics are lacking in certain areas, unfortunately.

Re:The Unsound Playground: Java and Scala's Type Systems are... by MrBuddyCasino on Monday November 28, 2016 @04:09AM (#13054283)
Generics in Java are pretty complicated already, so much so that very few developers fully understand them. I'm not sure the gain in complexity would be worth the added safety.

I'd like to see examples in languages where this is possible, if there are any?

Re:The Unsound Playground: Java and Scala's Type Systems are... by int_19h on Sunday November 27, 2016 @04:28PM (#13051638)
It is both.

Passing collections of subtypes where a read-only collection of supertypes is expected by the receiving code is fine, because the receiving code can't do anything that would break type safety.

But Java decided to only have a single array type, as a matter of expediency. But they also wanted covariance, because, as you point out, sometimes it's just convenient - more expediency. And so the trade-off was to enforce safety at runtime.

It's still a defect in a sense that the design itself could be made sound, if there were a read-only array reference type in Java.

Re:The Unsound Playground: Java and Scala's Type Systems are... by marvy on Sunday November 27, 2016 @11:27PM (#13053400)
Read-only is too strict. Can't reverse the array, or sort it.
Re:The Unsound Playground: Java and Scala's Type Systems are... by int_19h on Monday November 28, 2016 @03:31AM (#13054157)
Sure you can - you just need to copy it.

And I didn't mean to imply that array references should always be read-only. What I meant is that there should be a way to say "the referenced array cannot be mutated through this reference", like const* in C. 90% of the time, when you pass or return arrays, this is exactly what you want.

Re:The Unsound Playground: Java and Scala's Type Systems are... by nradov on Sunday November 27, 2016 @08:48PM (#13052868)
The existence of arrays at all is a trade off. The language would be more type safe and more consistent with OO best practices without arrays and using dynamic random access lists instead. But that would be too slow for some applications.
Re:The Unsound Playground: Java and Scala's Type Systems are... by emmelaich on Sunday November 27, 2016 @05:42PM (#13052061)
I think it is a fundamental.

Just because D isa B it does not mean list(D) isa list(B).

Re:The Unsound Playground: Java and Scala's Type Systems are... by prodigal_erik on Sunday November 27, 2016 @07:45PM (#13052631)
What we need is

  listContaining(Dog) isa listContaining(Animal)
  listAccepting(Animal) isa listAccepting(Dog)
Re:The Unsound Playground: Java and Scala's Type Systems are... by STRML on Sunday November 27, 2016 @09:11PM (#13052944)
Which is precisely the idea of covariant getters / contravariant setters in some sounder languages.
Re:The Unsound Playground: Java and Scala's Type Systems are... by Kreeg on Monday November 28, 2016 @06:36AM (#13054789)
IIRC, Scala has covariant/contravariant generics

(apologies for the bad terminology, I'm not too familiar with the details)

Re:The Unsound Playground: Java and Scala's Type Systems are... by Groxx on Wednesday November 30, 2016 @12:28AM (#13068695)
support for "<? extends X>" and "<? super X>" is supporting covariant / contravariant. invariant would be "<x>".

basically. the same concept applies in a number of areas, with varying support (e.g. Java doesn't support contravariance in its method overloading: http://stackoverflow.com/a/2501513/16295 )

Re:The Unsound Playground: Java and Scala's Type Systems are... by quotemstr on Sunday November 27, 2016 @06:25PM (#13052256)
> Is this a defect or a tradeoff?

I'm curious what line of thinking could prompt this question. Not all trade-offs are good choices just because they're trade-offs! Maybe it has something to do with the recent spate of language design choices justified with "it was a trade-off", as if the words "trade-off" is an incantation that puts a technical decision above criticism.

Re:The Unsound Playground: Java and Scala's Type Systems are... by marvy on Sunday November 27, 2016 @11:32PM (#13053415)
> Not all trade-offs are good choices just because they're trade-offs.

Indeed. But they're not automatically bad choices either. Are you saying this one is a bad trade-off?

Re:The Unsound Playground: Java and Scala's Type Systems are... by plaguuuuuu on Sunday November 27, 2016 @05:58PM (#13052122)
Both defect and trade-off. I can definitely see value in being able to pass an array of Dogs and treat it as an array of Animals, but it means that the array of Animals is not really an array of animals (because certain properties of a real array of Animals actually cause the code to break) and as a result the language is a little dirtier for it. Like, that's pretty horrible to be honest.

What you really want in that instance is to be able to easily and explicitly cast the Dog[] to Animal[] before calling the method. A better language would make this easy instead. I think Java's Vector type can do it, so it surprises me that they allow that sort of type weirdness with arrays..

Re:The Unsound Playground: Java and Scala's Type Systems are... by marvy on Sunday November 27, 2016 @11:29PM (#13053405)
I think you're wrong about Vector