From forax at univ-mlv.fr Fri Dec 9 14:56:04 2011 From: forax at univ-mlv.fr (=?ISO-8859-1?Q?R=E9mi_Forax?=) Date: Fri, 09 Dec 2011 23:56:04 +0100 Subject: array and diamond Message-ID: <4EE29204.2020008@univ-mlv.fr> Is there a reason why the diamond syntax can't be used with an array ? List[] list = new List<>[12]; R?mi From maurizio.cimadamore at oracle.com Sat Dec 10 03:38:22 2011 From: maurizio.cimadamore at oracle.com (maurizio cimadamore) Date: Sat, 10 Dec 2011 11:38:22 +0000 Subject: array and diamond In-Reply-To: <4EE29204.2020008@univ-mlv.fr> References: <4EE29204.2020008@univ-mlv.fr> Message-ID: <4EE344AE.5010807@oracle.com> On 09-Dec-11 10:56 PM, R?mi Forax wrote: > Is there a reason why the diamond syntax can't be used > with an array ? > > List[] list = new List<>[12]; > Because the current inference rules would end up inferring: List[] list = new List[12]; If you special-cased diamond on arrays so that it is inferred as: List[] list = new List[12]; Then it would be safe. Maurizio > R?mi > From david.holmes at oracle.com Sun Dec 11 03:16:55 2011 From: david.holmes at oracle.com (David Holmes) Date: Sun, 11 Dec 2011 21:16:55 +1000 Subject: array and diamond In-Reply-To: <4EE29204.2020008@univ-mlv.fr> References: <4EE29204.2020008@univ-mlv.fr> Message-ID: <4EE49127.9040907@oracle.com> On 10/12/2011 8:56 AM, R?mi Forax wrote: > Is there a reason why the diamond syntax can't be used > with an array ? > > List[] list = new List<>[12]; It can in Java 7. That's the bug that we just discussed in regard to the java.util.concurrent code cleanup where I suggested to use: HashEntry[] newTable = new HashEntry<>[1]; to avoid the explicit cast. ;-) David > R?mi > > From forax at univ-mlv.fr Sun Dec 11 07:21:23 2011 From: forax at univ-mlv.fr (=?ISO-8859-1?Q?R=E9mi_Forax?=) Date: Sun, 11 Dec 2011 16:21:23 +0100 Subject: array and diamond In-Reply-To: <4EE344AE.5010807@oracle.com> References: <4EE29204.2020008@univ-mlv.fr> <4EE344AE.5010807@oracle.com> Message-ID: <4EE4CA73.5070903@univ-mlv.fr> On 12/10/2011 12:38 PM, maurizio cimadamore wrote: > On 09-Dec-11 10:56 PM, R?mi Forax wrote: >> Is there a reason why the diamond syntax can't be used >> with an array ? >> >> List[] list = new List<>[12]; >> > Because the current inference rules would end up inferring: > > List[] list = new List[12]; > > If you special-cased diamond on arrays so that it is inferred as: > > List[] list = new List[12]; > > Then it would be safe. but in that case List[] list = new List[12]; is not equivalent to static List[] foo() { ... } ... List[] list = foo(); because as you said, Object will be inferred. Is there another case where diamond inference behave differently from method inference ? > > Maurizio R?mi From forax at univ-mlv.fr Sun Dec 11 07:26:17 2011 From: forax at univ-mlv.fr (=?ISO-8859-1?Q?R=E9mi_Forax?=) Date: Sun, 11 Dec 2011 16:26:17 +0100 Subject: array and diamond In-Reply-To: <4EE49127.9040907@oracle.com> References: <4EE29204.2020008@univ-mlv.fr> <4EE49127.9040907@oracle.com> Message-ID: <4EE4CB99.8020104@univ-mlv.fr> On 12/11/2011 12:16 PM, David Holmes wrote: > On 10/12/2011 8:56 AM, R?mi Forax wrote: >> Is there a reason why the diamond syntax can't be used >> with an array ? >> >> List[] list = new List<>[12]; > > It can in Java 7. That's the bug that we just discussed in regard to > the java.util.concurrent code cleanup where I suggested to use: > > HashEntry[] newTable = new HashEntry<>[1]; > > to avoid the explicit cast. ;-) > > David I've put my desktop in a Faraday cage and cut the internet link to be sure there will be no auto-update even over the air :) R?mi From maurizio.cimadamore at oracle.com Sun Dec 11 08:43:39 2011 From: maurizio.cimadamore at oracle.com (Maurizio Cimadamore) Date: Sun, 11 Dec 2011 16:43:39 +0000 Subject: array and diamond In-Reply-To: <4EE4CA73.5070903@univ-mlv.fr> References: <4EE29204.2020008@univ-mlv.fr> <4EE344AE.5010807@oracle.com> <4EE4CA73.5070903@univ-mlv.fr> Message-ID: <4EE4DDBB.1040207@oracle.com> On 11/12/11 15:21, R?mi Forax wrote: > On 12/10/2011 12:38 PM, maurizio cimadamore wrote: >> On 09-Dec-11 10:56 PM, R?mi Forax wrote: >>> Is there a reason why the diamond syntax can't be used >>> with an array ? >>> >>> List[] list = new List<>[12]; >>> >> Because the current inference rules would end up inferring: >> >> List[] list = new List[12]; >> >> If you special-cased diamond on arrays so that it is inferred as: >> >> List[] list = new List[12]; >> >> Then it would be safe. > > but in that case > List[] list = new List[12]; > is not equivalent to > static List[] foo() { ... } > ... > List[] list = foo(); > because as you said, Object will be inferred. > > Is there another case where diamond inference behave differently from > method inference ? No - that's why the array case is banned; after going back and forth we decided to adopt the principle of least surprise, which means diamond always behave as a static generic method (the diamond spec is also design to map the diamond case back to the generic factory method). What I was saying is that in order for diamond to be safely used in array context we'd need to revisit both the spec and the compiler, and to rethink the diamond vs. generic method inference strategy. I think it will be much more productive if we could remove the ban about creating an array of List (reification) - which then would mean that the current inference strategy will apply to arrays as well. Maurizio > >> >> Maurizio > > R?mi >