[Proposal]

Daniel Keep daniel.keep.lists at gmail.com
Sun Jun 18 21:10:29 PDT 2006



Sjoerd van Leent wrote:
> Dave wrote:
>>
>> public class Test
>> {
>>     public static void main(String args[])
>>     {
>>         Integer i = 100;
>>         System.out.println(sqr(i));
>>         int j = 1000;
>>         System.out.println(sqr(j));
>>     }
>>     public static <T> T sqr(T x)
>>     {
>>         return x * x;
>>     }
>> }
>>
>> However, I get this when I compile:
>>
>> Test.java:10: operator * cannot be applied to T,T
>>                 return x * x;
>>
>> ?
>>
>> Thanks,
>>
>> - Dave
> 
> 
> This is correct behaviour. You are now stating:
> 
> T must be of type Object. Type object doesn't have the * operator
> implemented. Even extending it from Number won't help, since the *
> operator doesn't work on class instances, only on primitives. To get it
> work you need quite a hack:
> 
> package generic.test;
> 
> import sun.reflect.generics.reflectiveObjects.NotImplementedException;
> 
> public class Test {
>     public static void main(String args[]) {
>         Integer i = 100;
>         System.out.println(sqr(i));
>         int j = 1000;
>         System.out.println(sqr(j));
>     }
>     public static <T extends Number> T sqr(T x) {
>         if(x instanceof Integer) {
>             return (T)(Number)new Integer(x.intValue() * x.intValue());
>         } else if(x instanceof Byte) {
>             return (T)(Number)new Byte((byte)(x.byteValue() *
> x.byteValue()));
>         } else if(x instanceof Long) {
>             return (T)(Number)new Long(x.longValue() * x.longValue());
>         } else if(x instanceof Double) {
>             return (T)(Number)new Double(x.doubleValue() *
> x.doubleValue());
>         } else if(x instanceof Float) {
>             return (T)(Number)new Float(x.floatValue() * x.floatValue());
>         } else {
>             throw new NotImplementedException();
>         }
>     }
> }
> 
> Which is, if you ask me, not the best way of using Generics, well, I
> didn't invent them in Java, and it shows that it is really syntactic sugar.
> 
> Regards,
> Sjoerd

Aah, "generics"...

I remember a fairly important language person (obviously can't remember
the damn name *grumble*) was having a go at .NET 2.0's generics for
exactly the same reason.

This basically cripples generics in both Java and .NET since it makes
them utterly useless for any kind of numerical work: there are no
interfaces or base classes for arithmetic operations, which is a
complete pain in the arse.

Generics in .NET and Java should really be called Object Generics, since
it's all but useless for basic types.

Of course, D doesn't suffer this problem because its templates simply
get expanded until it becomes clear that it can't be done, and THEN it
fails.  .NET and Java seem to treat templates more like compile-time
object casting.

I suppose the one advantage that .NET has over Java *and* C++/D is that
its templates are compiled without being expanded first.  This means
that arbitrary libraries can come along and instantiate the templates,
even without the source code.  AFAIK, Java does what C++ and D do, which
is to expand all the templates on compilation.

Ah well.  Not our problem, after all :)  Just another wart to add to
Java's list ^_^

	-- Daniel

-- 
Unlike Knuth, I have neither proven or tried the above; it may not even
make sense.

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/



More information about the Digitalmars-d mailing list