Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to preserve the original type of a type-variable in Java?
    primarykey
    data
    text
    <p>I have the following example:</p> <pre><code>class bounds { private StringBuilder str = new StringBuilder(); public &lt;Type&gt; void add (Type value) { add_specific (value); str.append (String.format("%n")); } private &lt;Type extends Number&gt; void add_specific (Type value) { str.append (value); } public String toString () { return str.toString(); } public static void main (String[] args) { bounds test = new bounds(); test.add (new Integer (42)); System.out.print (test.toString()); } } </code></pre> <p>When I try to compile it I get the following error:</p> <pre> bounds.java:7: error: method add_specific in class bounds cannot be applied to given types; add_specific (value); ^ required: Type#1 found: Type#2 reason: inferred type does not conform to declared bound(s) inferred: Type#2 bound(s): Number where Type#1,Type#2 are type-variables: Type#1 extends Number declared in method add_specific(Type#1) Type#2 extends Object declared in method add(Type#2) 1 error </pre> <p>This looks to me as if the original type of the argument passed to the <code>add</code> method gets lost in the body of <code>add</code>. How can I preserve the type so that the correct <code>add_specific</code> method can be chosen?</p> <p><strong>Update</strong></p> <p>I have simplified my example, because I thought it would be easier to understand. But it seems to me that most people do not understand the reason why it contains a generic and a specific function. So I paste a more advanced example. Maybe this makes the reasons more obvious:</p> <pre><code>import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; class bounds { private StringBuilder str = new StringBuilder(); public &lt;Type&gt; void add (Type value) { add_specific (value); str.append (String.format("%n")); } private &lt;Type extends Number&gt; void add_specific (Type value) { str.append (value); } private void add_specific (String value) { str.append ('"'); for (int i = 0; i &lt; value.length(); i++) { char ch = value.charAt(i); switch (ch) { case '\\': str.append ("\\\\"); break; case '"': str.append ("\\\""); break; default: str.append (ch); } } str.append ('"'); } private static DateFormat iso8601 = new SimpleDateFormat("'\"'yyyy-MM-dd'T'HH:mm:ssZ'\"'"); private void add_specific (Date date) { str.append (iso8601.format(date)); } public String toString () { return str.toString(); } public static void main (String[] args) { bounds test = new bounds(); test.add (new Integer (42)); test.add ("42"); test.add (new Date()); System.out.print (test.toString()); } } </code></pre> <p>I have a generic function called <code>add</code>. This generic function does something generic and calls a specific function to do something specific. The problem is, that the type to select the specific function gets lost in the generic function. And the question is how to fix this? How do I have to write the generic function so that it is still possible to select the right specific function in the body of the generic function?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload