POV-Ray : Newsgroups : povray.off-topic : A question about Java generics (not a flame) Server Time
6 Nov 2024 12:20:08 EST (-0500)
  A question about Java generics (not a flame) (Message 1 to 10 of 63)  
Goto Latest 10 Messages Next 10 Messages >>>
From: Warp
Subject: A question about Java generics (not a flame)
Date: 9 May 2008 05:34:12
Message: <48241a93@news.povray.org>
Yes, I know it would be better to ask this in a Java forum, but I'm just
too lazy to dig one up (as well as not too eager to show my ignorance in
a random forum ;) ). Given the amount of programmers here, I'm sure at
least someone knows the answer to this.

  In C++, if you have a very complicated template type, such as for example:

std::map<std::string, std::vector<std::vector<std::string> > >

it would be quite laborious to have to write that type every single time
when you need it. The solution to this is to create a type alias:

typedef std::map<std::string, std::vector<std::vector<std::string> > >
    TheMap;

  Now it's much shorter to use that type. For example:

TheMap m;
for(TheMap::iterator iter = m.begin(); iter != m.end(); ++iter)
    ...

  Java supports generics, which are instantiated with a syntax very similar
to this. However, Java does not support type aliases.

  Is there a way in Java to shorten those long type names?

-- 
                                                          - Warp


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: A question about Java generics (not a flame)
Date: 9 May 2008 13:17:40
Message: <48248734@news.povray.org>
Warp wrote:
>   Is there a way in Java to shorten those long type names?
> 

Not that I know of. In fact, it's even more annoying in that you have to
type the whole type twice when creating an object.

Map<String, ArrayList<ArrayList<String>>> theMap =
   new HashMap<String, ArrayList<ArrayList<String>>>();
ArrayList<ArrayList<String>> someList = new ArrayList<ArrayList<String>>();

Of course, you could also use interfaces for the type and generic
parameters, only using a real class for the actual object you're creating,
making it a bit shorter:

Map<String, List<List<String>>> theMap =
   new HashMap<String, List<List<String>>>();
List<List<String>> someList = new ArrayList<List<String>>();

//and let's continue the example
List<String> deeperList = new ArrayList<String>();
List<String> otherDeeperList = new ArrayList<String>();
deeperList.add("one");
deeperList.add("two");
otherDeeperList.add("three");
otherDeeperList.add("four");
someList.add(deeperList);
someList.add(otherDeeperList);
theMap.put("foo", someList);

It's interesting to note that in scripting languages, all that could be a
one-liner. In Javascript:
var theMap = {"foo": [["one", "two"], ["three", "four"]]};

And even more interesting to note that C++0x will allow an initializer
syntax almost as short as that :)


Post a reply to this message

From: Warp
Subject: Re: A question about Java generics (not a flame)
Date: 9 May 2008 13:41:09
Message: <48248cb5@news.povray.org>
Nicolas Alvarez <nic### [at] gmailcom> wrote:
> Warp wrote:
> >   Is there a way in Java to shorten those long type names?
> > 

> Not that I know of.

  I was thinking if it's not possible even through inheritance.
Something like (iirc Java syntax):

class TheMap extends Map<String, ArrayList<ArrayList<String>>>
{};

  Then just use TheMap? (Of course from a purely OO point of view this
could be seen as a misuse of inheritance, but...)

> In fact, it's even more annoying in that you have to
> type the whole type twice when creating an object.

  I will never understand why they decided to drop 'typedef' when they
designed Java in the first place. It's so handy in C++.

> It's interesting to note that in scripting languages, all that could be a
> one-liner. In Javascript:
> var theMap = {"foo": [["one", "two"], ["three", "four"]]};

  Yeah, but that's only possible in languages with weak typing (I think
that's the term?) In other words, in languages where you don't have to
specify types explicitly, as they are inferred from the values.

> And even more interesting to note that C++0x will allow an initializer
> syntax almost as short as that :)

  I also wonder if the new 'auto' feature in C++0x will help alleviating
this nuisance a bit. At least it will allow writing things like:

auto p = std::make_pair(value1, value2);

  You don't have to even care what is the type returned by std::make_pair
or the types of those values, which is cool.

-- 
                                                          - Warp


Post a reply to this message

From: Orchid XP v8
Subject: Re: A question about Java generics (not a flame)
Date: 9 May 2008 13:48:38
Message: <48248e76$1@news.povray.org>
Warp wrote:
>   Yes, I know it would be better to ask this in a Java forum, but I'm just
> too lazy to dig one up (as well as not too eager to show my ignorance in
> a random forum ;) ). Given the amount of programmers here, I'm sure at
> least someone knows the answer to this.

[insert ironic comment here]

>   Java supports generics.

...OK, I just learned something.

>   Is there a way in Java to shorten those long type names?

Given the statement above, is there any point in my answering this? ;-)

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

From: Orchid XP v8
Subject: Re: A question about Java generics (not a flame)
Date: 9 May 2008 13:54:36
Message: <48248fdc@news.povray.org>
Warp wrote:

>   I will never understand why they decided to drop 'typedef' when they
> designed Java in the first place. It's so handy in C++.

Well, in Java everything is a class. So it's not 100% obvious where 
you'd *put* a typedef. (I suppose it could be something that's local to 
a class or something...)

>> It's interesting to note that in scripting languages, all that could be a
>> one-liner. In Javascript:
>> var theMap = {"foo": [["one", "two"], ["three", "four"]]};
> 
>   Yeah, but that's only possible in languages with weak typing (I think
> that's the term?) In other words, in languages where you don't have to
> specify types explicitly, as they are inferred from the values.

Two cases here:

- JavaScript is dynamically typed. Types become known only at runtime. 
Types are checked only at runtime. Many scripting languages follow this 
model. (Smalltalk immediately springs to mind here.)

- Haskell is statically typed, but types are [usually] inferred 
automatically. Types are checked at compile-time only. Types are not 
known at runtime at all. [With the exception of types belonging to a 
class of some kind.]



Obviously nobody cares, but:

   theMap = fromList [("foo", [["one", "two"], ["three", "four"]])]

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

From: Warp
Subject: Re: A question about Java generics (not a flame)
Date: 9 May 2008 14:29:08
Message: <482497f4@news.povray.org>
Orchid XP v8 <voi### [at] devnull> wrote:
> Warp wrote:

> >   I will never understand why they decided to drop 'typedef' when they
> > designed Java in the first place. It's so handy in C++.

> Well, in Java everything is a class. So it's not 100% obvious where 
> you'd *put* a typedef.

  You'd put it in the exact same place as you'd put a class.

-- 
                                                          - Warp


Post a reply to this message

From: Warp
Subject: Re: A question about Java generics (not a flame)
Date: 9 May 2008 14:34:49
Message: <48249949@news.povray.org>
Orchid XP v8 <voi### [at] devnull> wrote:
> >   Java supports generics.

> ...OK, I just learned something.

  Java generics are a bit ironical because before they existed the
common consensus among Java people was that C++ templates are an
abomination (in the same category as, for example, multiple inheritance,
if not even worse).

  Of course this was before all the generic programming craze, and when
it hit, the Java people had to swallow their pride and introduce some
crippled template mechanism into Java. Naturally since templates were
always considered an abomination they couldn't call them that, so they
tried to save even a bit of their pride by renaming them to "generics".
(I suppose "generics" rides more on the whole "generic programming"
thing.)

  Of course Java generics are a bit crippled because they don't support
basic types (such as int). Thus they solve only part of the problem which
Java had before.

-- 
                                                          - Warp


Post a reply to this message

From: Orchid XP v8
Subject: Re: A question about Java generics (not a flame)
Date: 9 May 2008 14:50:19
Message: <48249ceb$1@news.povray.org>
Warp wrote:
> Orchid XP v8 <voi### [at] devnull> wrote:
>>>   Java supports generics.
> 
>> ...OK, I just learned something.
> 
>   Java generics are a bit ironical because before they existed the
> common consensus among Java people was that C++ templates are an
> abomination (in the same category as, for example, multiple inheritance,
> if not even worse).
> 
>   Of course this was before all the generic programming craze, and when
> it hit, the Java people had to swallow their pride and introduce some
> crippled template mechanism into Java. Naturally since templates were
> always considered an abomination they couldn't call them that, so they
> tried to save even a bit of their pride by renaming them to "generics".
> (I suppose "generics" rides more on the whole "generic programming"
> thing.)
> 
>   Of course Java generics are a bit crippled because they don't support
> basic types (such as int). Thus they solve only part of the problem which
> Java had before.

Well, actually Eiffel calls it generics too. I don't know when Eiffel 
was designed, but it's had generics from day 1, and that has always been 
its name.

I don't actually know how C++ templates work, but my take on it is this:

- Generics is the ability of a programming language to implement classes 
that are parameterised over another class.

- C++ implement this ability using templates.

- Templates can do other things besides implementing generics.

So to say that Java's generics is a crippled copy of C++'s template 
mechanism isn't entirely true.

OTOH, I haven't seen Java generics or C++ templates "for real", so maybe 
I'm mistaken.

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

From: Warp
Subject: Re: A question about Java generics (not a flame)
Date: 9 May 2008 15:00:54
Message: <48249f66@news.povray.org>
Orchid XP v8 <voi### [at] devnull> wrote:
> - Templates can do other things besides implementing generics.

  I think it suffices to say that with C++ templates you can, for example,
calculate at *compilation time* whether a certain number is prime or not.

  Moreover, you can even do that without using any integer literals or
any basic type, such as 'int'. (Although doing it like that is really
not the most efficient way.)

-- 
                                                          - Warp


Post a reply to this message

From: Orchid XP v8
Subject: Re: A question about Java generics (not a flame)
Date: 9 May 2008 15:15:21
Message: <4824a2c9$1@news.povray.org>
>> - Templates can do other things besides implementing generics.
> 
>   I think it suffices to say that with C++ templates you can, for example,
> calculate at *compilation time* whether a certain number is prime or not.

So... it's Turing-complete?

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

Goto Latest 10 Messages Next 10 Messages >>>

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.