Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #6405 > unrolled thread

Problem with generics and dynamic array copy

Started bySebastian <sebastian@undisclosed.invalid>
First post2011-07-22 19:26 +0200
Last post2011-07-22 15:49 -0700
Articles 8 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Problem with generics and dynamic array copy Sebastian <sebastian@undisclosed.invalid> - 2011-07-22 19:26 +0200
    Re: Problem with generics and dynamic array copy Sebastian <sebastian@undisclosed.invalid> - 2011-07-22 19:30 +0200
      Re: Problem with generics and dynamic array copy markspace <-@.> - 2011-07-22 11:23 -0700
        Re: Problem with generics and dynamic array copy Sebastian <sebastian@undisclosed.invalid> - 2011-07-22 20:36 +0200
          Re: Problem with generics and dynamic array copy Robert Klemme <shortcutter@googlemail.com> - 2011-07-22 21:27 +0200
            Re: Problem with generics and dynamic array copy lewbloch <lewbloch@gmail.com> - 2011-07-22 13:33 -0700
            Re: Problem with generics and dynamic array copy Sebastian <sebastian@undisclosed.invalid> - 2011-07-22 22:37 +0200
              Re: Problem with generics and dynamic array copy lewbloch <lewbloch@gmail.com> - 2011-07-22 15:49 -0700

#6405 — Problem with generics and dynamic array copy

FromSebastian <sebastian@undisclosed.invalid>
Date2011-07-22 19:26 +0200
SubjectProblem with generics and dynamic array copy
Message-ID<j0cbru$289$1@news.albasani.net>
Hello there,

how can I make the compile error go away? The marked line is wrong:
Type mismatch: cannot convert from Class<capture#2-of ?> to Class<T>

   /**
    * Copies the specified array to a new array with the same component
    * type and length as the given array.
    * @param <T> the type of the array elements
    * @param src the array to be copied
    * @return the copy
    */
   public static final <T> T[] arraycopy( T[] src )
   {
     Class<T> componentType = src.getClass().getComponentType(); // !!!!
     @SuppressWarnings("unchecked")
     T[] dest = (T[]) Array.newInstance( componentType, src.length );
     System.arraycopy( src, 0, dest, 0, src.length );
     return dest;
   }

I'd be grateful for a hint.
-- Sebastian

[toc] | [next] | [standalone]


#6406

FromSebastian <sebastian@undisclosed.invalid>
Date2011-07-22 19:30 +0200
Message-ID<j0cc2t$289$2@news.albasani.net>
In reply to#6405
Am 22.07.2011 19:26, schrieb Sebastian:
> Hello there,
>
> how can I make the compile error go away? The marked line is wrong:
> Type mismatch: cannot convert from Class<capture#2-of ?> to Class<T>
>
> /**
> * Copies the specified array to a new array with the same component
> * type and length as the given array.
> * @param <T> the type of the array elements
> * @param src the array to be copied
> * @return the copy
> */
> public static final <T> T[] arraycopy( T[] src )
> {
> Class<T> componentType = src.getClass().getComponentType(); // !!!!
> @SuppressWarnings("unchecked")
> T[] dest = (T[]) Array.newInstance( componentType, src.length );
> System.arraycopy( src, 0, dest, 0, src.length );
> return dest;
> }
>
> I'd be grateful for a hint.
> -- Sebastian

to be mor exact: of course I could just cast componentType to Class<T>.
But why should that be necessary in view of the declaration of src?
Is the thinking behind the method wrong?

-- Sebastian

[toc] | [prev] | [next] | [standalone]


#6408

Frommarkspace <-@.>
Date2011-07-22 11:23 -0700
Message-ID<j0cf75$8pm$1@dont-email.me>
In reply to#6406
On 7/22/2011 10:30 AM, Sebastian wrote:

>> public static final <T> T[] arraycopy( T[] src )
>> {
>> Class<T> componentType = src.getClass().getComponentType(); // !!!!


RTFM.  getComponentType() returns Class<?>, not Class<T>.

<http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#getComponentType%28%29>

public Class<?> getComponentType()

     Returns the Class representing the component type of an array. If 
this class does not represent an array class this method returns null.

[toc] | [prev] | [next] | [standalone]


#6409

FromSebastian <sebastian@undisclosed.invalid>
Date2011-07-22 20:36 +0200
Message-ID<j0cfva$cmc$1@news.albasani.net>
In reply to#6408
Am 22.07.2011 20:23, schrieb markspace:
> On 7/22/2011 10:30 AM, Sebastian wrote:
>
>>> public static final <T> T[] arraycopy( T[] src )
>>> {
>>> Class<T> componentType = src.getClass().getComponentType(); // !!!!
>
>
> RTFM. getComponentType() returns Class<?>, not Class<T>.
>
> <http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#getComponentType%28%29>
>
>
> public Class<?> getComponentType()
>
> Returns the Class representing the component type of an array. If this
> class does not represent an array class this method returns null.
>

well, yes, but given that src.getClass() must give one the class object
for arrays with component type T, why is the compiler not smart enough 
to infer that the unknown class parameter in the return value of
getComponentType() must be T? As a human I can see that, that's why I
can cast to Class<T>, but I don't believe that I'm smarter than javac...

-- Sebastian

[toc] | [prev] | [next] | [standalone]


#6411

FromRobert Klemme <shortcutter@googlemail.com>
Date2011-07-22 21:27 +0200
Message-ID<98u18jF6l7U1@mid.individual.net>
In reply to#6409
On 22.07.2011 20:36, Sebastian wrote:
> Am 22.07.2011 20:23, schrieb markspace:
>> On 7/22/2011 10:30 AM, Sebastian wrote:
>>
>>>> public static final <T> T[] arraycopy( T[] src )
>>>> {
>>>> Class<T> componentType = src.getClass().getComponentType(); // !!!!
>>
>>
>> RTFM. getComponentType() returns Class<?>, not Class<T>.
>>
>> <http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#getComponentType%28%29>
>>
>>
>>
>> public Class<?> getComponentType()
>>
>> Returns the Class representing the component type of an array. If this
>> class does not represent an array class this method returns null.
>>
>
> well, yes, but given that src.getClass() must give one the class object
> for arrays with component type T, why is the compiler not smart enough
> to infer that the unknown class parameter in the return value of
> getComponentType() must be T? As a human I can see that, that's why I
> can cast to Class<T>, but I don't believe that I'm smarter than javac...

The compiler has no idea what the semantics of getComponentType() is. 
It could be implemented as

public Class<?> getComponentType() { return Object.class; }

and still be conformant to the declaration.  Hence it cannot do any 
automatic inference based on the fact you know that the array is T[]. 
Btw, you can actually pass B[] where B is a subclass of T.

Since Array.newInstance() accepts Class<?> you should simply use that - 
that cast to T[] is needed anyway.

Of course, even better you scrap your implementation and use

http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html#copyOf%28T[],%20int%29

Cheers

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

[toc] | [prev] | [next] | [standalone]


#6414

Fromlewbloch <lewbloch@gmail.com>
Date2011-07-22 13:33 -0700
Message-ID<f614944f-3a6c-41d6-9897-53f4da03cfa3@v11g2000prn.googlegroups.com>
In reply to#6411
On Jul 22, 12:27 pm, Robert Klemme <shortcut...@googlemail.com> wrote:
> On 22.07.2011 20:36, Sebastian wrote:
>
>
>
>
>
>
>
>
>
> > Am 22.07.2011 20:23, schrieb markspace:
> >> On 7/22/2011 10:30 AM, Sebastian wrote:
>
> >>>> public static final <T> T[] arraycopy( T[] src )
> >>>> {
> >>>> Class<T> componentType = src.getClass().getComponentType(); // !!!!
>
> >> RTFM. getComponentType() returns Class<?>, not Class<T>.
>
> >> <http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#get...>
>
> >> public Class<?> getComponentType()
>
> >> Returns the Class representing the component type of an array. If this
> >> class does not represent an array class this method returns null.
>
> > well, yes, but given that src.getClass() must give one the class object
> > for arrays with component type T, why is the compiler not smart enough
> > to infer that the unknown class parameter in the return value of
> > getComponentType() must be T? As a human I can see that, that's why I
> > can cast to Class<T>, but I don't believe that I'm smarter than javac...
>
> The compiler has no idea what the semantics of getComponentType() is.
> It could be implemented as
>
> public Class<?> getComponentType() { return Object.class; }
>
> and still be conformant to the declaration.  Hence it cannot do any
> automatic inference based on the fact you know that the array is T[].
> Btw, you can actually pass B[] where B is a subclass of T.
>
> Since Array.newInstance() accepts Class<?> you should simply use that -
> that cast to T[] is needed anyway.
>
> Of course, even better you scrap your implementation and use
>
> http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html#co...[],%20int%29
>

I'd say _best_ yet.  In general, unless it really, really doesn't do
what you need, stick to the standard API instead of reinventing it.

This does, of course, require that you be aware of the API, but things
like 'java.util.Arrays' had better be part of your standard vocabulary
in any event.

Otherwise you have some upgrading to do on your skills.

--
Lew

[toc] | [prev] | [next] | [standalone]


#6416

FromSebastian <sebastian@undisclosed.invalid>
Date2011-07-22 22:37 +0200
Message-ID<j0cn2k$u6p$1@news.albasani.net>
In reply to#6411
Am 22.07.2011 21:27, schrieb Robert Klemme:
> On 22.07.2011 20:36, Sebastian wrote:
>> Am 22.07.2011 20:23, schrieb markspace:
>>> On 7/22/2011 10:30 AM, Sebastian wrote:
>>>
>>>>> public static final <T> T[] arraycopy( T[] src )
>>>>> {
>>>>> Class<T> componentType = src.getClass().getComponentType(); // !!!!
>>>
>>>
>>> RTFM. getComponentType() returns Class<?>, not Class<T>.
>>>
>>> <http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#getComponentType%28%29>
>>>
>>>
>>>
>>>
>>> public Class<?> getComponentType()
>>>
>>> Returns the Class representing the component type of an array. If this
>>> class does not represent an array class this method returns null.
>>>
>>
>> well, yes, but given that src.getClass() must give one the class object
>> for arrays with component type T, why is the compiler not smart enough
>> to infer that the unknown class parameter in the return value of
>> getComponentType() must be T? As a human I can see that, that's why I
>> can cast to Class<T>, but I don't believe that I'm smarter than javac...
>
> The compiler has no idea what the semantics of getComponentType() is. It
> could be implemented as
>
> public Class<?> getComponentType() { return Object.class; }
>
> and still be conformant to the declaration. Hence it cannot do any
> automatic inference based on the fact you know that the array is T[].
> Btw, you can actually pass B[] where B is a subclass of T.
>
> Since Array.newInstance() accepts Class<?> you should simply use that -
> that cast to T[] is needed anyway.
>
> Of course, even better you scrap your implementation and use
>
> http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html#copyOf%28T[],%20int%29
>
>
> Cheers
>
> robert
>
Thanks. Java has a confusing variety of ways to copy an array:
  - the copyOf and copyOfRange methods in Arrays
  - System.arraycopy
  - clone and do a cast

I'll go with your recommendation.

-- Sebastian
	

[toc] | [prev] | [next] | [standalone]


#6432

Fromlewbloch <lewbloch@gmail.com>
Date2011-07-22 15:49 -0700
Message-ID<8d967dd6-a043-4e28-a9a3-6a158791ecba@d8g2000prf.googlegroups.com>
In reply to#6416
On Jul 22, 1:37 pm, Sebastian <sebast...@undisclosed.invalid> wrote:
> Am 22.07.2011 21:27, schrieb Robert Klemme:
>
>
>
>
>
>
>
> > On 22.07.2011 20:36, Sebastian wrote:
> >> Am 22.07.2011 20:23, schrieb markspace:
> >>> On 7/22/2011 10:30 AM, Sebastian wrote:
>
> >>>>> public static final <T> T[] arraycopy( T[] src )
> >>>>> {
> >>>>> Class<T> componentType = src.getClass().getComponentType(); // !!!!
>
> >>> RTFM. getComponentType() returns Class<?>, not Class<T>.
>
> >>> <http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#get...>
>
> >>> public Class<?> getComponentType()
>
> >>> Returns the Class representing the component type of an array. If this
> >>> class does not represent an array class this method returns null.
>
> >> well, yes, but given that src.getClass() must give one the class object
> >> for arrays with component type T, why is the compiler not smart enough
> >> to infer that the unknown class parameter in the return value of
> >> getComponentType() must be T? As a human I can see that, that's why I
> >> can cast to Class<T>, but I don't believe that I'm smarter than javac...
>
> > The compiler has no idea what the semantics of getComponentType() is. It
> > could be implemented as
>
> > public Class<?> getComponentType() { return Object.class; }
>
> > and still be conformant to the declaration. Hence it cannot do any
> > automatic inference based on the fact you know that the array is T[].
> > Btw, you can actually pass B[] where B is a subclass of T.
>
> > Since Array.newInstance() accepts Class<?> you should simply use that -
> > that cast to T[] is needed anyway.
>
> > Of course, even better you scrap your implementation and use
>
> >http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html#co...[],%20int%29
>
> > Cheers
>
> > robert
>
> Thanks. Java has a confusing variety of ways to copy an array:
>   - the copyOf and copyOfRange methods in Arrays
>   - System.arraycopy
>   - clone and do a cast
>
> I'll go with your recommendation.

'clone()' and cast is the least optimal.  'System.arraycopy()' is the
"old" way.  'Arrays.copyOf()' is the "new" way and most typesafe, and
is somewhat easier to use than the others.

--
Lew

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web