The Unsound Playground: Java and Scala's Type Systems are Unsound
I ran the example and I got the same result. The result that is provided, and the one I got, makes sense. Can someone explain to me what is &quot;broken&quot; about this?<p>Broken down you:<p>Create 3 classes
   - Unsound: Unsound contains a static generic method. Two types are requested (T and U). T is the parameter and the U is the return type. From now on T will be called CoerceInputType and U will be called CoerceOutputType
   - Constrain: A class that has two generic types it requests. A being the first type, and B who is the second type. A restriction is placed on B saying that B extends A. We will call A ConstraintType and B ExtendedConstraintType
   - Bind: A class that takes one generic parameter of type A. Here we will call A BindType<p>What is happening here?<p>Your execution stack:<p><pre><code>   - *Call* Unsound.coerce. Tell it CoerceInputType is an Integer and CoerceOutputType is a String. You pass into coerce a value of &#x27;0&#x27; with the type CoerceInputType (an Integer class) and accept a result with the return type CoerceOutputType (a String class) 
   - *Assign* In Unsound.coerce you define a type Constraint&lt;CoerceOutputType, ANYTHING extends CoerceInputType&gt;. This means that these type annotations will be propagated through anything this is passed into. 
   - *Assign* you create an instance of bind with the type CoerceOutputType.
</code></pre>
What this currently looks like<p><pre><code>   &lt;Output, AnyType that extends Input&gt; Output ourFunction(Input i) {
      return i
   } 
</code></pre>
What happens: You get an exception as Integer is not castable to a String. You can look further down to the only link in this page to see where that cast is coming from in the bytecode.<p>The authors seem to be surprised that this will not execute? Or maybe that this doesnt? This is completely expected behavior. Maybe not to someone who hasen&#x27;t worked in Java and is coming from another VM but for me this is not surprising. Why? Here&#x27;s a list:<p>The ways you can trigger a type change:<p><pre><code>   1. Auto boxing
   2. Casting
</code></pre>
For integers and strings, these two type conversions will not work. Here is a much more direct explanation of what&#x27;s happening here:<p><pre><code>   class Test {
      public static void main(String[] args) {
         String a = &quot;Hello World&quot;;
         Integer b = 10;
         a = b;
      }
   }
</code></pre>
Running this you will get the following error:<p><pre><code>   test.java:5: error: incompatible types: Integer cannot be converted to String
   		a = b;
   		    ^
   1 error
</code></pre>
This is because Integer cannot auto (un)box to String. This exception happens at compile time. I&#x27;ll get into why in a bit.<p>You might also ask, &quot;But gravypod, what about an explicit cast, will that work?&quot; No, it will not!<p><pre><code>   test.java:5: error: incompatible types: Integer cannot be converted to String
   		a =(String) b;
   		            ^
   1 error
</code></pre>
This will not work at compile time as well.<p>Now for the next section: why will this not be picked up at compile time? Well that&#x27;s a hard one to answer, the basic answer is the method isn&#x27;t existent yet. Or to be closer to the truth, the method&#x27;s types don&#x27;t exist yet. When you use generics you&#x27;re creating a method that will work on a base set of types. There isn&#x27;t necessarily hard type checking on generics though. These are mainly there for the programmer, not the runtime. The runtime will operate on any data and is just expecting casting to change between types.<p>When run though a decompiler this is the output of Unsound.class: <a href="http:&#x2F;&#x2F;hastebin.com&#x2F;sitamomogu.java" rel="nofollow">http:&#x2F;&#x2F;hastebin.com&#x2F;sitamomogu.java</a><p>All that has happened is the compiler couldn&#x27;t follow the type annotations at compile time and it was left for runtime. This should be improved, but I don&#x27;t see what is broken. The time of the error just shifted. This is the same thing that happens with null. The error is not compile time when you are going to have an NPE. Instead they are runtime.<p>Here is example code that compiles perfectly fine:<p><pre><code>   public class Test {
   	public static void main(String[] args) {
   		String a = null;
   		System.out.println(a.getClass().getSimpleName());
   	}
   }
</code></pre>
When run you will get this exception:<p><pre><code>   Exception in thread &quot;main&quot; java.lang.NullPointerException
   	at Test.main(Test.java:7)
</code></pre>
I don&#x27;t think that this is broken behavior as this is consistent with the way the JVM is meant to operate. Types are suggestions to the VM. This to me is a side effect of auto (un)boxing.<p>Maybe I&#x27;m wrong. I hope someone could tell me why if so.<p>Edit: Please read <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=13051036" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=13051036</a> as this explanation is much more elegant and gets the idea across in a simpler way.