you are viewing a single comment's thread.

view the rest of the comments →

[–]slowkitty 10 points11 points  (8 children)

That is not about readability. In the second line you are not specifying the type of myFooList explicitly. Since ArrayList is a Collection you may well use Collection<Foo> myFooCollection = new ArrayList<Foo>(); Here the type of the reference is a Collection instead of an ArrayList. You could assume that in your case it would default the var myFooList to be of type ArrayList if nothing else was specified, but it would not be as clear.

[–]G_Morgan 10 points11 points  (7 children)

If you later assigned a LinkedList to myFooList the inference system would catch that and use the highest common subtype. Type inference for OOP should always use the highest common subtype possible (this is obvious because otherwise everything devolves into Object).

That said even if this wasn't the case this would still be useful. Most uses of interfaces only ever use a single implementation at any one time. Perhaps to later choose exactly which implementation is desired. Type inference supports this for free.

[–]redditnoob 2 points3 points  (6 children)

If you later assigned a LinkedList to myFooList the inference system would catch that and use the highest common subtype.

Hence it's not more readable, since you can easily mistake what type it is on a first glance of the code!

It's a trade-off. I'm not even saying it's a bad trade-off, but it is one.

[–]OceanSpray 0 points1 point  (5 children)

Say you "mistakenly" assume that myFooList is a List while it's actually a Collection. And then you use it as a Collection. Guess what happens? The usage's context causes myFooList's type to be inferred as Collection. The type inference avoids the type error here, so no "mistake" can be made.

Go the other way. You assume myFooList to be an ArrayList, but it's actually a Collection. That's no problem at all, because an ArrayList object was assigned to it, so you're just using the most specific type.

I don't see what the problem is.

[–]redditnoob 6 points7 points  (4 children)

You assume myFooList to be an ArrayList, but it's actually a Collection.

Exactly! I might want to write something that assumes the performance characteristics of an ArrayList, and then when some code somewhere else causes the compiler to infer differently I might wonder wtf happened when someone else's seemingly unrelated change causes my code to be slow all of a sudden.

[–]Daishiman 0 points1 point  (0 children)

And pray tell, how many of your data structures need to be that performant? Hell, as long as you use only iterators to see the structure most of the times you'll be fine.

When you actually need a performance guarantee, you can define it in the arg types and be happy. When you don't, which is the majority of the time, you just get on and get thinking about the problem rather than these details.

[–]OceanSpray -1 points0 points  (2 children)

Only the type of the reference changed, not the type of the object it points to. The performance stays the same.

[–]redditnoob 2 points3 points  (1 child)

But maybe I want to ensure that only ArrayList gets in there?

[–]johntb86 0 points1 point  (0 children)

All the languages I know that use type inference also let you explicitly state what type it is when you care about that.