Java Type Inference Sucks

Apr 15, 2008 17:28

Why doesn't this work?! Boo Java.

Variable unpacked_var =
(this_copy.unpackedVar == other.unpackedVar ?
this_copy.unpackedVar :
throw new IllegalStateException("Unhandled casee"));

java

Leave a comment

Comments 3

extensionality April 15 2008, 23:15:42 UTC
Is throw a statement or an expression? Does Java have an injection from statements into expressions like C does? Does the ternary operator accept statements?

I wasn't aware that Java had any type inference. That's why you have to write out the full types of all your variable.

Reply

nolacoaster April 16 2008, 13:34:23 UTC
Okay, you're right. This blog post requires way more explanation than I gave.

You are absolutely right, throw is a statement and not an expression. The weird thing was that Eclipse was giving me a type error rather than a syntax error for the example above, which temporarily made me think it was an expression. Frankly, I don't see any reason why it shouldn't be an expression, but whatever.

Re type inference, well something like this wouldn't be considered type inference in any sophisticated fashion, but as far as 'determining the type of an expression from its sub-expressions' is type inference, then this qualifies.

Java DOES have a much more interesting form of 'type inference,' however, since Java5. A perfect example is the emptyList() method in the java.util.Collections class. emptyList() returns an immutable, empty list. The method signature is:

public static final List emptyList()

However, if I want to use this method, most of the time I do not have to explicitly parameterize the method, although it is legal:

List = ( ... )

Reply

nolacoaster April 16 2008, 13:40:02 UTC
Boo, livejournal ate my generics. This is what those three lines should have said:

public static final < T > List< T > emptyList()

List< Integer > _list = Collections.< Integer >emptyList();

List< Integer > _list = Collections.emptyList();

Reply


Leave a comment

Up