On 21 October 2012 23:48, Reinier Zwitserloot <reini...@gmail.com> wrote:
> On Monday, October 15, 2012 2:42:11 PM UTC+2, Simon Ochsenreither wrote: > >> >> Unfortunately, introducing this kind of concept is exactly the kind of >>> thing java does NOT do, and languages like Scala DO do: it's not a matter >>> of superiority, it's a matter of tradeoffs: If this Nothing exists, it >>> cannot actually be a subtype of Object; a List<?> cannot accept an instance >>> of List<Nothing>, because I can still put nulls in List<?>, and nulls are >>> not a valid value for Nothing. Hell, generics in general play havoc with >>> this: Using trickery I can make a List<Nothing> with nulls in it, and then >>> write return thisHackedList.get(0), which means in practice that Nothing >>> needs type checking (i.e. that would lead to a ClassCastException or >>> similar, even if .get(0) returns null). >>> >> >> Is this just theorizing? I'd love to see that code. >> >> > What code? It's trivial: > > List<Nothing> list = new ArrayList<Nothing>(); > list.add(null); //Barring compiler magic, this will compile and run > without error. > > or: > > List<Object> list = new ArrayList<Object>(); > list.add(null); > List<Nothing> list2 = (List<Nothing>)list; > Nothing aNothingVariableThatContainsValue = list2.get(0); // compiler or > VM magic required to throw the needed runtime exception here. > > or even: > > Nothing y = null; // Compiler magic required to make this invalid. > > No more than the pre-existing compiler "magic". It's no more possible to declare a variable of type `Nothing` than it is to declare it to be of type `void` - even though both are valid method return types. It's also not possible to add a variable of type 'Car' to a List<Ferrari>, not unless it's immutable in which case you'd get back a List<Car>. Exactly the same logic applies in adding a variable of type Null to a List<Nothing> -- You received this message because you are subscribed to the Google Groups "Java Posse" group. To post to this group, send email to javaposse@googlegroups.com. To unsubscribe from this group, send email to javaposse+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/javaposse?hl=en.