Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is meant by left most bound for generic type or a method and why was this policy chosen for type erasure? [duplicate]

Tags:

java

generics

From Angelika Langer's generic FAQ:

Eliding type parameters.

When the compiler finds the definition of a generic type or method, it removes all occurrences of the type parameters and replaces them by their leftmost bound, or type Object if no bound had been specified.

Edit: As I understood in this case leftmost bound means what it means literally i.e farthest to the left in the list of bounds. I want to understand why was this policy chosen?

like image 874
Geek Avatar asked Mar 08 '13 14:03

Geek


2 Answers

In this case "leftmost" is not technical terminology. It literally means "farthest to the left". It simply means that when the type parameter has several bounds (restrictions) like this:

T extends Callable<Long> & Runnable

the compiler replaces all occurances of the type parameter T with the bound that is farthest to the left (in this case that's Callable<Long>):

Meaning that, as in Angelika's example, this:

private T task1, task2; 

is replaced with this:

private Callable task1, task2; 
like image 169
JLRishe Avatar answered Sep 30 '22 13:09

JLRishe


There is no reason. They just need to pick one. The whole thing sucks. It's like asking which one of your children you want to save. You want to save all, duh. But they don't let you.

like image 44
irreputable Avatar answered Sep 30 '22 14:09

irreputable