News for hackers. Stuff that matters.
Re:The Unsound Playground: Java and Scala's Type Systems are... by dvt on Monday November 28, 2016 @01:41AM (#13053833)
This, imo, is actually a bigger deal than generics. Since type erasure does all kinds of "magic", it doesn't surprise me that there will end up some kind of (T)-type casts somewhere in the OP's bytecode.

But the fact that the following is unsound (and has been for a while) will always remain a mystery to me:

  public class Main {
  
  	class Animal {}
  	class Dog extends Animal {}
  	class Cat extends Animal {}
  
  	public static void main (String[] args)  {
  		Main x = new Main();
  		Dog[] doges = new Dog[10];
  		x.t(doges);
  	}
   
  	public void t(Animal[] bar) {
  		bar[0] = new Cat();
  		bar[0] = new Animal(); // also breaks
  	}
  
  }
The array type seems completely tractable and may be able to be caught by the compiler.
Re:The Unsound Playground: Java and Scala's Type Systems are... by krona on Monday November 28, 2016 @09:05AM (#13055443)
> The array type seems completely tractable and may be able to be caught by the compiler.

This isn't an error, it was a design decision: https://en.wikipedia.org/wiki/Covariance_and_contravariance_...

You could call it bad design, but fixing it would (imo) just make Java even worse than it already is.

Re:The Unsound Playground: Java and Scala's Type Systems are... by dvt on Monday November 28, 2016 @03:07PM (#13057961)
My point had nothing to do with the merits of covariant types, just tractability. It seems that the array's type is tractable so the compiler should (at least) give a warning.
Re:The Unsound Playground: Java and Scala's Type Systems are... by JoshTriplett on Monday November 28, 2016 @03:13AM (#13054111)
This would be fine if t's array bar only allowed mutation of the objects within the array, but didn't allow mutation of the array itself. Treating a Dog as an Animal works fine; however, you can only treat a Dog[] as a const Animal[], not a mutable Animal[].
Re:The Unsound Playground: Java and Scala's Type Systems are... by trendkill on Monday November 28, 2016 @01:54AM (#13053867)
A joke from my childhood:

  Man goes to doctor, 
  and says:
  
  "Gee, doc, it 
   really hurts 
   when I do this."

  Doctor replies:

  "Then don't do that."
Re:The Unsound Playground: Java and Scala's Type Systems are... by paulddraper on Monday November 28, 2016 @02:32AM (#13053983)
Two men stand on a bridge. The first leans on the railing, which gives way and sends him over the edge.

Horrified, the second looks around and spots a civil engineer. Engineer shrugs and says, "Don't do that."

Re:The Unsound Playground: Java and Scala's Type Systems are... by TeMPOraL on Monday November 28, 2016 @03:26AM (#13054143)
Ha ha. Funny joke. Now let's get back to discussing the future applications of JavaScript in air travel...

;).

Re:The Unsound Playground: Java and Scala's Type Systems are... by zphds on Tuesday November 29, 2016 @04:00PM (#13066247)
"Don't do that"
Re:The Unsound Playground: Java and Scala's Type Systems are... by mvindahl on Monday November 28, 2016 @04:08AM (#13054277)
On the positive side, these kind of errors tend to blow up immediately when the code is executed. Assuming that you have a semi-decent test suite, it won't make it to production. The language type system won't save your butt but your unit tests will.

There is a deeper discussion here; if you are writing good automatic tests anyway, which I hope you are, then of how much value is a rigorious type system? :)

Re:The Unsound Playground: Java and Scala's Type Systems are... by greydius on Monday November 28, 2016 @10:48AM (#13056136)
I agree that a comprehensive test suite is valuable, but I don't think it's safe to assume that errors like this will always be caught, no matter how good your tests are. It's not reasonable to write a test for every possible situation that could ever occur. In fact, it's a giant waste of time.

> of how much value is a rigorous type system?

It saves a lot of time on those tests you won't have to write and maintain just to ensure that you're getting your types correct. Strong static types are also good documentation. Some type systems (e.g. Haskell) can even explicitly indicate whether a procedure is pure or has effects. Static types also enable dispatch that can be resolved at compile-time, resulting in more efficient execution. And the list goes on.

Of course, advocates of languages without static types have heard and dismissed all these arguments before. I guess it's really just a matter of taste.

Re:The Unsound Playground: Java and Scala's Type Systems are... by mvindahl on Tuesday November 29, 2016 @04:36AM (#13061939)
I've heard both sides of the static types vs dynamic types argument a lot of times. I don't think there will ever be any kind of universal agreement but I find the discussion to be interesting.

For the record, I personally think a language like Typescript hit a sweet spot. Not a lot of mandatory ceremony but you have the choice to add types where it matters, e.g. at interface level. Also the duck typing saves you from inventing class names for all kind of almost-primitive types.

Re:The Unsound Playground: Java and Scala's Type Systems are... by dllthomas on Monday November 28, 2016 @10:52PM (#13060640)
> if you are writing good automatic tests anyway, which I hope you are

"Don't write tests. Generate them."