• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generic Program Warning

 
Author
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, trying to learn write generic programs- not quite like C++ templates...I have the following generic program.



I compiled it an got the following warning


Would you fix this warning using exception handling. JC
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can rid yourself of the warning by returning a Number and not a T.
The warning is coming from this line:

return (T) sum;

You declare T as a subclass of Number, and *sum* is a variable of type Number. A superclass is not always able to be cast to a subclass. (i.e. String is a subclass of Object, but you can't just cast any Object to a String). This code compiles without warning:

because any subclass can be referred to by a superclass reference.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler is complaining because you are casting a Number to a T. This makes sense since you declared the function to return a T which needs to extend Number, but how do you know that sum is really the same run-time type as T and not some other class that extends Number? Well perhaps you do, but the compiler cannot tell since sum was declared as a Number.

I'm not too familiar with templates, but I think you can declare sum as a T instead. The other option, as Garrett pointed out, is to declare the function to return a Number instead of a T.

Layne
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Layne Lund: I'm not too familiar with templates, but I think you can declare sum as a T instead.

This poses problems because :

sum = sum.doubleValue() + elem.doubleValue();

returns a double which cannot be autoboxed into a T. (I only know bc that was the first solution I tested).
 
There is no greater crime than stealing somebody's best friend. I miss you tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic