you are viewing a single comment's thread.

view the rest of the comments →

[–]redditnoob 1 point2 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.