java needs typedef

S

Steve Green

I was just doing a bit of coding for Java 1.5 and using generics; I am
rewriting an application I developed using the new features. I said it
before they implemented generics that they needed typedef and now that we
have generics I say again ... we need typedef ...

Which is really worse having to cast a return value on occasion or defining
HashMap<Key,Class> var = new HashMap<Key,Class>();

I try to use descriptive class names so they are longer than this.

We should be able to define an alias to Generic types.
For example:
typedef HashMap<Key,Class> MyHashMap;

Just a little frustrated,
Steve
 
A

AJ M.

dont be lazy. It is bad enough they added generics and autoboxing to Java.
It's beauty was in its simplicity.
 
J

Jeff Schwab

AJ said:
dont be lazy. It is bad enough they added generics and autoboxing to Java.
It's beauty was in its simplicity.

I don't believe the issue is one of "laziness." It's one of clarity and
abstraction.

Of course, language changes are inherently Evil. What alternatives to
typedef are available already? I would appreciate suggestions from this
community. Why was typedef not brought into the language along with
generics?
 
P

Peter Rilling

I agree.

By the same logic as the original thread, one could say that any high level
language is also a matter of "laziness" when compared to assembly language.
No one certainly needs a language like C++ or Java or C# when it all gets
compiled/interpreted to machine code in the end.

I come from .NET and have just begun learning Java. Although some of the
constructs are different, .NET has come real conveniences that are working
their way into the Java world, and vis versa.

In the end, the evolution of software and languages make for a better
development environment where we are all more productive.
 
T

Thomas Weidenfeller

AJ said:
It's beauty was in its simplicity.

Yes, it was. And then they decided to add some pet syntax, because
<voice style="parody" type="old lady">
"it is so much nicer" :-(
</voice>

All this has nothing to do with evolution of the state of the art. Java
has become part of the usual language "enhancement" cycle. A language is
"enhanced" up to the point were someone comes and says enough is enough
and creates something simpler. Then this new simpler language starts
being "enhanced" over time up to the point were it is at least as
complex as the predecessor. At that point yet another new simple
language comes up ...



/Thomas
 
T

Tor Iver Wilhelmsen

Steve Green said:
HashMap<Key,Class> var = new HashMap<Key,Class>();

That is nothing. Using the wildcard variants for generics is the real
pain, and typedef won't help there.
 
V

Vincent Cantin

Steve Green said:
I was just doing a bit of coding for Java 1.5 and using generics; I am
rewriting an application I developed using the new features. I said it
before they implemented generics that they needed typedef and now that we
have generics I say again ... we need typedef ...

Which is really worse having to cast a return value on occasion or
defining
HashMap<Key,Class> var = new HashMap<Key,Class>();

I try to use descriptive class names so they are longer than this.

We should be able to define an alias to Generic types.
For example:
typedef HashMap<Key,Class> MyHashMap;

Just a little frustrated,
Steve

Instead of using typedef, just use inheritence :

public class MyHashMap extends HashMap<Key,Class>
{
}

... and that's it.

Vincent
 
V

Vincent Cantin

Tor Iver Wilhelmsen said:
That is nothing. Using the wildcard variants for generics is the real
pain, and typedef won't help there.

An example, please ?
 
T

Tim Tyler

AJ M. said:
dont be lazy. It is bad enough they added generics and autoboxing to Java.
It's beauty was in its simplicity.

Java was never simple.

In that respect it resembles English - full of
difficult-to-learn irregularities, and exceptions.

The designers chose to borrow from C++ rather than Smalltalk in that area.
 
T

Tor Iver Wilhelmsen

Vincent Cantin said:
An example, please ?

I think what I was really thinking of was bounded types.

HashMap<? extends Key, ? extends Class> var = new HashMap<Key,Class>();

The advantage over your version is that var can be assigned a map with
parametrized types that aren't exactly Key or Class.

Also for parametrized methods:

public <L extends List<? extends Element>> L merge(L list1, L list2) {
....
}
 
J

Joona I Palaste

AJ M. said:
It's beauty was in its simplicity.

It's common to see "its" misspelled, but not as common to see it spelled
both wrong and right in the same sentence.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top