Java Forum / General / December 2007
Still no typedef
Robert M. Gary - 05 Dec 2007 16:27 GMT I"m looking at 1.6 right now and am surprised we still don't have the concept of typedef (as we do in C, C++). I use Generic's a lot and often find that declaring an iterator for a Generic can take two lines of code. In C++ we could take the big long string that includes the iterator class, all its Generics (we called the template params in C+ +) and make it a single word to be used throughout the program. I was so happy when Java finally decided to use Generics (since C++ has had them forever) but thought it was a mistake to not include typedefs at the same time.
-Robert
Hunter Gratzner - 05 Dec 2007 18:08 GMT > I"m looking at 1.6 right now and am surprised we still don't have the > concept of typedef (as we do in C, C++). So why don't you just use C++?
Daniel Pitts - 05 Dec 2007 18:12 GMT >> I"m looking at 1.6 right now and am surprised we still don't have the >> concept of typedef (as we do in C, C++). > > So why don't you just use C++? Not to mention, typedef has limited uses and doesn't actually improve code IMO.
 Signature Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
lord.zoltar@gmail.com - 05 Dec 2007 18:18 GMT On Dec 5, 1:12 pm, Daniel Pitts <newsgroup.spamfil...@virtualinfinity.net> wrote:
> >> I"m looking at 1.6 right now and am surprised we still don't have the > >> concept of typedef (as we do in C, C++). [quoted text clipped - 6 lines] > -- > Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/> I dunno... I thought they were somewhat useful, but to me they seemed mostly like syntactic sugar. Maybe I never saw any brilliant examples of usage.
I wonder if it would be possible to fake typedefs with Java annotations...
Andreas Leitgeb - 05 Dec 2007 19:13 GMT >> >> I"m looking at 1.6 right now and am surprised we still don't have the >> >> concept of typedef (as we do in C, C++). [quoted text clipped - 4 lines] > mostly like syntactic sugar. Maybe I never saw any brilliant examples > of usage. After reading Robert's post, I actually found myself agreeing to him, out of a sense of general typing lazyness.
If it were ever implemented, then either in terms of module-scoped shortcut syntax (vaguely the same approach as "import") e.g. this could look like: import HashMap<String,Integer> as SIHashMap; (not that it was even remotely likely to ever happen)
Another approach would be with a class that is auto-generated from the generic class (and derives from it), and has all the parameter types replaced by the given ones, and passes on all the c'tors to the generic original. This is something, a third-party tool could do, so one wouldn't depend on what the Java-gods decide... but then again, it would induce a little runtime-penalty (for the constructors), which is still quite a high price for some mere source convenience...
Maybe I better not mention approaches boiling down to a preprocess... (damn, too late :-)
> I wonder if it would be possible to fake typedefs with Java > annotations... I have no idea, how this could work.
Kira Yamato - 06 Dec 2007 13:35 GMT >>> I"m looking at 1.6 right now and am surprised we still don't have the >>> concept of typedef (as we do in C, C++). >> >> So why don't you just use C++? > Not to mention, typedef has limited uses and doesn't actually improve code IMO. You have to be kidding me. Code readability may not improve code performance, but it certainly improve maintainability.
 Signature -kira
Lew - 06 Dec 2007 14:01 GMT >>>> I"m looking at 1.6 right now and am surprised we still don't have the >>>> concept of typedef (as we do in C, C++). [quoted text clipped - 5 lines] > You have to be kidding me. Code readability may not improve code > performance, but it certainly improve maintainability. But the point is that "typedef" isn't considered by all to improve readability for Java code. You begged the question and set up a straw-man argument.
Why have you suddenly started talking about "performance" when everyone else was talking about source-code readability? (OK, writability - those talking readability seem to not like "typedef".)
 Signature Lew
Chris Dollin - 06 Dec 2007 14:03 GMT >>>>> I"m looking at 1.6 right now and am surprised we still don't have the >>>>> concept of typedef (as we do in C, C++). [quoted text clipped - 12 lines] > was talking about source-code readability? (OK, writability - those talking > readability seem to not like "typedef".) Count me as a counter-example to that claim. Being able to name things is Good. You can name for meaning, not structure.
 Signature Chris "countOfMonteChristo" Dollin
Hewlett-Packard Limited Cain Road, Bracknell, registered no: registered office: Berks RG12 1HN 690597 England
Arnauld.Loyer@gmail.com - 06 Dec 2007 14:15 GMT > >>>>> I"m looking at 1.6 right now and am surprised we still don't have the > >>>>> concept of typedef (as we do in C, C++). [quoted text clipped - 15 lines] > Count me as a counter-example to that claim. Being able to name things is > Good. You can name for meaning, not structure. I strongly agree with Chris Dollin
Patricia Shanahan - 06 Dec 2007 14:21 GMT >>>>>> I"m looking at 1.6 right now and am surprised we still don't have the >>>>>> concept of typedef (as we do in C, C++). [quoted text clipped - 12 lines] > Count me as a counter-example to that claim. Being able to name things is > Good. You can name for meaning, not structure. Same here, especially with an IDE that lets me see how the name is declared whenever I need to know that.
Patricia
Andreas Leitgeb - 06 Dec 2007 15:14 GMT >> (OK, writability - those talking readability seem to not like "typedef".) > Count me as a counter-example to that claim. Being able to name things is > Good. You can name for meaning, not structure. Me, four (after two more followup'ers) :-)
Lew - 05 Dec 2007 18:16 GMT >> I"m looking at 1.6 right now and am surprised we still don't have the >> concept of typedef (as we do in C, C++). > > So why don't you just use C++? You don't need typedef in Java, so there'd be no point to it.
C++ doesn't have generics, it has templates. Not even close to the same thing.
C# has generics, and they're reifiable. Except for where reifiability is important, they are a lot like Java generics.
Even being compile-time only, as Java generics are, they're still quite useful.
I have no idea what in blazes the OP is going on about with
> In C++ we could take the big long string that includes the > iterator class, all its Generics (we called the template params in C+ > +) and make it a single word to be used throughout the program. None of that remark made any sense to me.
Types in Java are already one word. Generic types have two, typically, not exactly a huge burden. Those "extra" words are not superfluous - they are the nut of why we use generics in the first place, in that they are the words that enforce type safety at compile time. If we want a "typedef" for a generic type, we /could/ just use the raw type. Of course, then we'd have to add whole extra *lines* of code to replace the type safety guaranteed by the generic types.
This win goes to Java.
Now if the OP could show in concrete terms what a "typedef" might be in Java, and how it would make any positive difference at all, we could have a discussion. I am not seeing it - neither a reasonable definition of what a "typedef" would be, nor how it would help.
 Signature Lew
Robert M. Gary - 05 Dec 2007 18:49 GMT > You don't need typedef in Java, so there'd be no point to it. > > C++ doesn't have generics, it has templates. Not even close to the same thing. Well, I don't really have time to teach you C++ templates in this thread but I think if you did some research you would have a better understanding of what they do. Besides, I already tought one class on this at the University you could have attended that or you can do your own research.
> Now if the OP could show in concrete terms what a "typedef" might be in Java, > and how it would make any positive difference at all, we could have a > discussion. I am not seeing it - neither a reasonable definition of what a > "typedef" would be, nor how it would help. I think as you gain experience in generics more in Java you will find that by the time you delare the iterator, pass it a few classes for its generic's parameters, then pass its contructor a container that requires a couple of class for its generics description you will find that declaring a single iterator can require more than one line of code. Typedefs make this much cleaner.
-Robert
Lew - 05 Dec 2007 19:26 GMT >> You don't need typedef in Java, so there'd be no point to it. >> [quoted text clipped - 5 lines] > this at the University you could have attended that or you can do your > own research. Quite all right, I have certainly used C++ templates plenty in my C++ programming.
>> Now if the OP could show in concrete terms what a "typedef" might be in Java, >> and how it would make any positive difference at all, we could have a [quoted text clipped - 7 lines] > that declaring a single iterator can require more than one line of > code. Typedefs make this much cleaner. I have been using generics for quite some time now. Since they came out, in fact. I also constantly read material on their use, including frequent re-reads of the Sun tutorials on the matter, and the JLS itself. My comments were informed by that experience. Not that your /ad hominem/ remarks either required a response or in any way addressed the points I made.
How about this declaration of an iterator and its use?
List <String> names = fillNames(); for ( String name : names ) { System.out.println( name ); }
Yep. Generics sure made that verbose.
As I pointed out, the "extra" use of "String" (ooh, that one-word overhead!) serves a purpose - to guarantee the type safety of the String operations.
You still haven't shown any example of how a Java "typedef" would work. How about in the scenario I presented?
 Signature Lew
Andreas Leitgeb - 05 Dec 2007 20:02 GMT > How about this declaration of an iterator and its use? > List <String> names = fillNames(); > for ( String name : names ) If all the uses were that short and simple, then there indeed would not be any desire for a typedef-alike at all :-)
Even if there were typedefs, anyone using them for that example should be ... (* enter punishment of your choice :-) *)
Lew - 05 Dec 2007 20:24 GMT >> How about this declaration of an iterator and its use? >> List <String> names = fillNames(); [quoted text clipped - 5 lines] > Even if there were typedefs, anyone using them for that > example should be ... (* enter punishment of your choice :-) *) I feel that the burden of proof is on the typedef proponents to show how it would serve in a more complex example. Nevertheless, I provided such an example and how it actually does not favor typedef, in another part of this thread.
 Signature Lew
Chris Dollin - 06 Dec 2007 09:44 GMT >>> How about this declaration of an iterator and its use? >>> List <String> names = fillNames(); [quoted text clipped - 10 lines] > example and how it actually does not favor typedef, in another part of this > thread. The umpteenth time I wrote
Map<Node, Integer>
in recent code I gave up and wrote a subclass of HashMap<Node, Integer> with a short [but meaningful] name and used that instead, vaguely confident that no-one else was going to want to pass in their own Map<Node, Integer>.
Vaguely.
Had I been able to write something akin to
type Indicies = Map<Node, Integer>;
and used Indicies as the type I would have given audible thanks.
Being able to give names to things is a fundamental programming abstraction.
 Signature Chris "next up: expressions" Dollin
Hewlett-Packard Limited registered office: Cain Road, Bracknell, registered no: 690597 England Berks RG12 1HN
lord.zoltar@gmail.com - 06 Dec 2007 14:43 GMT > The umpteenth time I wrote > [quoted text clipped - 16 lines] > -- > Chris "next up: expressions" Dollin Man I totally hear ya! I know I typed ArrayList<GenericPair<String, ArrayList<String[]>>> waaaay too many times yesterday.
typedef ArrayList<GenericPair<String, ArrayList<String[]>>> NamedTable;
would have been nice. I supposed I could write a wrapper class:
class NamedTable { private ArrayList<GenericPair<String, ArrayList<String[]>>> table;
...
}
but then I get stuck writing wrappers to all the accessor methods...
It's just syntactic sugar for type synonyms, but maybe this would be a good use of typedefs. I never used generics like that in C++ so it never mattered then.
Robert Klemme - 25 Dec 2007 22:19 GMT > Man I totally hear ya! > I know I typed > ArrayList<GenericPair<String, ArrayList<String[]>>> > waaaay too many times yesterday. That could well be an indication that you do not use proper abstractions.
Cheers
robert
Arne Vajhøj - 24 Dec 2007 23:20 GMT >> You don't need typedef in Java, so there'd be no point to it. >> [quoted text clipped - 5 lines] > this at the University you could have attended that or you can do your > own research. There are not much point in following a a class in programming with a teacher which is so incompetent in programming that he is not aware of the big differences between C++ templates and Java generics.
Arne
Lew - 25 Dec 2007 14:43 GMT >>> C++ doesn't have generics, it has templates. Not even close to the >>> same thing. [quoted text clipped - 9 lines] > is not aware of the big differences between C++ templates > and Java generics. Based on certain internal evidence in the statement, whether he actually ever "tought [sic]" at "the" or any other "University" lacks a degree of credibility.
 Signature Lew
Robert M. Gary - 05 Dec 2007 18:51 GMT > > I"m looking at 1.6 right now and am surprised we still don't have the > > concept of typedef (as we do in C, C++). > > So why don't you just use C++? Ok, so if there are a lot of nice things in Java but one nice thing in C++ you would prefer to use C++? I'm not following your logic. I've been a software development architect for about 15 years now, there are some places where C++ is nicer but a logical person would not throw out the baby with the bath water.
-Robert
Arnauld.Loyer@gmail.com - 05 Dec 2007 19:25 GMT I don't think it's a debate for the best language, just that declaring some variable using generics can be quite long.
Map<String, Container<Entity, Comparator<Entity>>> myVariable=... Iterator<Container<Entity, Comparator<Entity>>> iterator = myVariable.values().iterator();
suppose the definition of Entity contains also some generics : Entity becomes Entity<Node, RendererType>
the declaration of the Iterator becomes :
Iterator<Container<Entity<Node, RendererType>, Comparator<Entity<Node, RendererType>>>> iterator = ...
which becomes quite long :(
using a kinf of type def :
typedef Entity<Node, RendererType> nodeEntity; typedef Container<nodeEntity, Comparator<nodeEntity>> containerEntity;
the iterator becomes : Iterator<containerEntity> iterator = ... shorter and more readable
Arnauld
Lew - 05 Dec 2007 19:29 GMT > I don't think it's a debate for the best language, just that declaring > some variable using generics can be quite long. [quoted text clipped - 17 lines] > > typedef Entity<Node, RendererType> nodeEntity; Type names should start with an upper-case letter.
> typedef Container<nodeEntity, Comparator<nodeEntity>> containerEntity; > > the iterator becomes : > Iterator<containerEntity> iterator = ... > shorter and more readable public void shortcutMethod() { class NodeEntity extends Entity <Node, Renderer> {} NodeEntity entity; ... }
Only works with extendable classes, of course, and has consequences.
But why give up the documentation value of the generic types? That seems like false economy to me.
 Signature Lew
Wojtek - 05 Dec 2007 19:38 GMT Arnauld.Loyer@gmail.com wrote :
> I don't think it's a debate for the best language, just that declaring > some variable using generics can be quite long. Ten years ago I would completely agree with you.
But not now. Modern IDEs remove the "I had to do a lot of typing" issue with context sensitive variable and class help.
So the length of the line is moot. But the readability is sure improved. Rather than chasing down where the typedef was defined to see what it meant, you simply read the line. And it could be buried in any of the many included header files.
And I did do C programming for many years.
 Signature Wojtek :-)
Andreas Leitgeb - 05 Dec 2007 19:51 GMT > Arnauld.Loyer@gmail.com wrote : >> I don't think it's a debate for the best language, just that declaring >> some variable using generics can be quite long. > But not now. Modern IDEs remove the "I had to do a lot of typing" issue Maybe they save the typing, but they don't help with reading over that bulk lateron :-)
> So the length of the line is moot. But the readability is sure > improved. Rather than chasing down where the typedef was defined Well, as you mentioned "Modern IDE"s: right-click -> view-definition (or simply with vim: "<Ctrl>-]" and <Ctrl-T> to jump back, afterwards)
> And it could be buried in any > of the many included header files. Fortunately not in Java :-)
Lew - 05 Dec 2007 20:19 GMT Wojtek <nowhere@a.com> wrote:
>> And it could be buried in any >> of the many included header files.
> Fortunately not in Java :-) Imports could be just as bad. If the typedef were importable, it'd be no more useful for self-documenting code than a raw type is.
I don't think the verbosity of generic types is wasted or redundant or unnecessary or even undesirable. The type arguments are the /raison d'être/ of generics, and they do often "alias" to single-letters, at least in the declarations:
public class Foo< T extends Comparable<? super T> > { T member; // all the benefits of typedef in here }
OK, let's return to the iterator example, which is verbose, I agree.
List <Foo<Comparator<Integer>> stuff = new ArrayList <Foo<Comparator<Integer>> ();
Iterator <Foo<Comparator<Integer>> iter = stuff.iterator();
for ( Foo<Comparator<Integer> foo; iter.hasNext(); ) { foo = iter.next(); // phew, by hear at least all that verbiage is gone }
Of course, the syntactic sugar in this particular use case is already present in the enhanced for loop. so typedef would only be useful for other iterator scenarios, such as when you remove() or add() through an iterator. Let's pretend this is that.
The typedef would add one line of code and reduce the generic overhead from four repetitions to one.
typedef FooInt Foo<Comparator<Integer> FooInt;
List <FooInt> FooList stuff = new ArrayList <FooInt> ();
Iterator <FooInt> iter = stuff.iterator();
for ( FooInt foo; iter.hasNext(); ) { foo = iter.next(); // phew, by hear at least all that verbiage is gone }
Of course, that only got rid of part of the overhead. Now we need another typedef to get rid of the FooInt.
typedef FooInt Foo<Comparator<Integer> FooInt; typedef List <FooInt> FooIntList;
FooIntList stuff = new ArrayList <FooInt> ();
Iterator <FooInt> iter = stuff.iterator();
for ( FooInt foo; iter.hasNext(); ) { foo = iter.next(); // phew, by hear at least all that verbiage is gone }
Huh, that only got one out at the cost of one, plus a whole typedef line. The same would happen for ArrayList <FooInt> and Iterator <FooInt>. Plus all these typedef names would start to be an overhead of their own, having to remember their meaning as well as the more common "List", "ArrayList" and "Iterator".
List <Foo<Comparator<Integer>> stuff = new ArrayList <Foo<Comparator<Integer>> ();
Iterator <Foo<Comparator<Integer>> iter = stuff.iterator();
for ( Foo<Comparator<Integer> foo; iter.hasNext(); ) { foo = iter.next(); // phew, by hear at least all that verbiage is gone }
In certain ways the original is clearer, at least to me. The words "List", "ArrayList", "Iterator" and "Foo" leap out at me, and the generic stuff remains noise until I focus on it, at which point it provides a nice safe feeling of matching types. So in casual reading I see the raw types, and algorithm is perfectly readable. In detailed reading the detail is only slightly more dense than what a typedef would show.
So to typedefs in Java, I say, "Pllpppllplpllpppttthh!"
 Signature Lew
Wojtek - 05 Dec 2007 22:33 GMT Lew wrote :
> Wojtek <nowhere@a.com> wrote: >>> And it could be buried in any of the many included header files. [quoted text clipped - 3 lines] > Imports could be just as bad. If the typedef were importable, it'd be no > more useful for self-documenting code than a raw type is. But header file can be really evil. Not only for typedefs, but also for macros (#defines). You really need to know what is in a header file, or else your program will behave in bizarre ways (I may have the syntax wrong, its been a while):
------------------------- // no ints allowed! typedef int long typedef LONG long -------------------------
And before someone says "WHY would anyone do this?", I would reply that it does happen. Someone started out with an int, and decided that a long was needed, but was too lazy to go through all the code.
Now you as a maintenance programmer must find out just why an int is 64 bits long.
 Signature Wojtek :-)
Daniel Pitts - 05 Dec 2007 22:45 GMT > Lew wrote : >> Wojtek <nowhere@a.com> wrote: [quoted text clipped - 22 lines] > Now you as a maintenance programmer must find out just why an int is 64 > bits long. I believe you just made "long" a reference to the type "int" and then to the type "LONG".
I also don't think you can override types like that.
Although, the point about #define is a good one.
The truth is that someone started out with an int, but should have used a specific value type that avoids primitive obsession. <http://virtualinfinity.net/wordpress/program-design/2007/10/28/primitive-obsession/>
 Signature Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Wojtek - 06 Dec 2007 01:02 GMT Daniel Pitts wrote :
>> Lew wrote : >>> Wojtek <nowhere@a.com> wrote: [quoted text clipped - 33 lines] > specific value type that avoids primitive obsession. > <http://virtualinfinity.net/wordpress/program-design/2007/10/28/primitive-obsession/> Ok, then:
#define int long #define LONG long
:-)) I did not use typdef when I was programming in C. At the time it seemed like an overly complex way of doing things.
 Signature Wojtek :-)
Wojtek - 06 Dec 2007 01:05 GMT Daniel Pitts wrote :
>> Lew wrote : >>> Wojtek <nowhere@a.com> wrote: [quoted text clipped - 33 lines] > specific value type that avoids primitive obsession. > <http://virtualinfinity.net/wordpress/program-design/2007/10/28/primitive-obsession/> Ok, then:
#define int long #define LONG long
:-)) I did not use typdef when I was programming in C. At the time it seemed like an overly complex way of doing things.
And you are right, there is no LONG on the C lexicon.
 Signature Wojtek :-)
Andreas Leitgeb - 06 Dec 2007 08:57 GMT > I did not use typdef when I was programming in C. At the time it seemed > like an overly complex way of doing things. In C, the typedef was necessary for not having to use the keyword "struct" all that often.
In C++, this is no longer the case, but typedef is still used to define abstract system type names like time_t, pid_t,... and also to give a simple name to complicated type constructs.
the latter would also be good use in Java.
Andreas Leitgeb - 05 Dec 2007 23:01 GMT > I don't think the verbosity of generic types is wasted or redundant or > unnecessary or even undesirable. The type arguments are the /raison d'être/ > of generics, In my own opinion, the /raison d'être/ of generics is, that one writes code once, "generically", and the user can still use it in a type-safe way on a number of different Client-classes. It's certainly not the particular syntax that completely describes the nature of the assembly at every place of usage. One can also give a name to a class, instead of completely re-specifying it whereever one uses it. So why shoud it now be bad (apart from that it's not possible in current and near-future Java) to give a name to a certain generics-assembly?
> OK, let's return to the iterator example, which is verbose, I agree. > List <Foo<Comparator<Integer>> stuff [quoted text clipped - 3 lines] > The typedef would add one line of code and reduce the generic overhead from > four repetitions to one. That's it.
> typedef FooInt Foo<Comparator<Integer>> FooInt; huh? why two FooInt's?
> Of course, that only got rid of part of the overhead. Now we need another > typedef to get rid of the FooInt. No need to get rid of that. If it's small enough, we really don't need to overdo it.
Patricia Shanahan - 05 Dec 2007 23:16 GMT ...
>> typedef FooInt Foo<Comparator<Integer>> FooInt; > huh? why two FooInt's? [quoted text clipped - 3 lines] > No need to get rid of that. If it's small enough, we really don't need to > overdo it. "typedef" is not currently a Java keyword. Here's an alternative syntax suggestion:
class FooInt Foo<Comparator<Integer>>;
The existing class declaration syntax does not allow the identifier to be immediately followed by another identifier. Declaration scope could be controlled by the same rules as for existing class declarations.
Patricia
Daniel Pitts - 05 Dec 2007 23:18 GMT > .... >>> typedef FooInt Foo<Comparator<Integer>> FooInt; [quoted text clipped - 15 lines] > > Patricia I think I would prefer interface over class for this.
 Signature Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Patricia Shanahan - 06 Dec 2007 00:03 GMT >> .... >>>> typedef FooInt Foo<Comparator<Integer>> FooInt; [quoted text clipped - 17 lines] >> > I think I would prefer interface over class for this. Perhaps it ought to tell the truth - use "interface" if Foo<Comparator<Integer>> is an interface, "class" if it is a class.
Patricia
Andreas Leitgeb - 06 Dec 2007 00:07 GMT >> Here's an alternative syntax suggestion: >> class FooInt Foo<Comparator<Integer>>; > I think I would prefer interface over class for this. No, not really, afterall one should also be able to do FooInt x=new FooInt(42); lateron. IMHO, at least.
Patricia Shanahan - 06 Dec 2007 00:10 GMT >>> Here's an alternative syntax suggestion: >>> class FooInt Foo<Comparator<Integer>>; [quoted text clipped - 3 lines] > FooInt x=new FooInt(42); > lateron. IMHO, at least. My alternative "tell the truth" proposal would allow this if FooInt is a class. On the other hand, if it is an interface one should be able to say:
class SomeClass implements FooInt{ ... }
Patricia
Daniel Pitts - 06 Dec 2007 19:56 GMT >>> Here's an alternative syntax suggestion: >>> class FooInt Foo<Comparator<Integer>>; [quoted text clipped - 3 lines] > FooInt x=new FooInt(42); > lateron. IMHO, at least. Right, but FooInt is an interface to the class Foo<Comparator<Integer>>.
All that it *really* means is FooInt is an alias for Foo<Comparator<Integer>>. Perhaps calling it a Type Alias is better than a TypeDef.
Now that I think more about it, type aliases are another form of abstraction/indirection that might be useful, especially if they are implemented as more than typedefs in c++...
Imaging this:
class MyClass { /* The following says, ListType is the specific type we need, and is assignable to List<FooBar>. The fact that it is ArrayList is "hidden" from clients. I'm assuming types would be static members automatically, but if there were a way to have instance-specific types, that would be even more "interesting". */ public type ListType implements List<FooBar> = ArrayList<FooBar>(); private ListType list; public void setBackingList(ListType list) { this.list = list; }
public ListType getList() { return list; } }
class OtherClass { public void things() { MyClass mc = new MyClass(); mc.setBackingList(new MyClass.ListType()); MyClass.ListType lt = mc.getList(); // lt's compile time type is List<FooBar> & MyClass.ListType. MyClass.ListType doesn't expose ArrayList methods.} }
This allows for a kind of interface that you can instantiate. Don't mind me, random babbling and stream-of-consciousness dump.
 Signature Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Steve Wampler - 06 Dec 2007 00:16 GMT > class FooInt Foo<Comparator<Integer>>; This isn't my area so feel free to tell me what I'm doing wrong, but how would this be different than:
class FooInt extends Foo<Comparator<Integer>> {}
Wouldn't this be legal already?
Similarly for:
interface BazDouble extends Baz<Comparator<Double>> {}
 Signature Steve Wampler -- swampler@noao.edu The gods that smiled on your birth are now laughing out loud.
Patricia Shanahan - 06 Dec 2007 00:46 GMT >> class FooInt Foo<Comparator<Integer>>; > [quoted text clipped - 8 lines] > > interface BazDouble extends Baz<Comparator<Double>> {} FooInt would only have a default constructor, regardless of the Foo<Comparator<Integer>> constructors.
Patricia
John W. Kennedy - 06 Dec 2007 01:07 GMT >>> class FooInt Foo<Comparator<Integer>>; >> [quoted text clipped - 11 lines] > FooInt would only have a default constructor, regardless of the > Foo<Comparator<Integer>> constructors. And Foo might be final in the first place.
 Signature John W. Kennedy "Never try to take over the international economy based on a radical feminist agenda if you're not sure your leader isn't a transvestite." -- David Misch: "She-Spies", "While You Were Out"
Steve Wampler - 06 Dec 2007 01:47 GMT >>> This isn't my area so feel free to tell me what I'm doing wrong, but >>> how would this be different than: [quoted text clipped - 11 lines] > > And Foo might be final in the first place. Ah - got it. Thanks to both of you!
 Signature Steve Wampler -- swampler@noao.edu The gods that smiled on your birth are now laughing out loud.
Patricia Shanahan - 06 Dec 2007 02:21 GMT >>>> This isn't my area so feel free to tell me what I'm doing wrong, but >>>> how would this be different than: [quoted text clipped - 13 lines] > > Ah - got it. Thanks to both of you! More subtly, in the typedef approach I would expect FooInt.class to be the same object as Foo.class. For purposes of reflection and debug, FooInt really is Foo<Comparator<Integer>>, not one of its subclasses.
Patricia
Lew - 06 Dec 2007 02:23 GMT >>>> This isn't my area so feel free to tell me what I'm doing wrong, but >>>> how would this be different than: [quoted text clipped - 9 lines] >>> FooInt would only have a default constructor, regardless of the >>> Foo<Comparator<Integer>> constructors. Nonsense. You just define the constructors in the inherited class.
>> And Foo might be final in the first place. Might be. Usually isn't.
Given that Java does not, at this time, sport a "typedef" keyword, the class-derivation technique will work in quite many cases. In fact, it's a very common idiom. There are an awful lot of heritable classes to which this would apply.
class Dao<T extends Entity> {...} class User extends Entity {...} class UserDao extends Dao<User> { ... stuff specific to User manipulation }
Of course, here we're doing much more than a typedef, we're actually defining overrides and other specific behaviors for a User-level Dao class. We get the compactness for free, this time.
 Signature Lew
Patricia Shanahan - 06 Dec 2007 02:36 GMT >>>>> This isn't my area so feel free to tell me what I'm doing wrong, but >>>>> how would this be different than: >>>>> >>>>> class FooInt extends Foo<Comparator<Integer>> {} >>>>> >>>>> Wouldn't this be legal already? ...
>>>> FooInt would only have a default constructor, regardless of the >>>> Foo<Comparator<Integer>> constructors. > > Nonsense. You just define the constructors in the inherited class. If defined the way Steve suggested, in the quoted message, FooInt indeed only has a default constructor.
Assuming non-final Foo, FooInt could be defined to have a constructor matching each accessible Foo constructor. However, defining FooInt is then potentially much more complicated than with the typedef-like idea.
Patricia
Piotr Kobzda - 06 Dec 2007 16:39 GMT > "typedef" is not currently a Java keyword. Here's an alternative syntax > suggestion: > > class FooInt Foo<Comparator<Integer>>; How about a bit different syntax for /type aliasing/ (good term?):
class FooInt = Foo<Comparator<Integer>>;
?
Of course, "tell the truth" rule is still needed, e.g.:
interface IntComparator = Comparator<Integer>; class FooInt = Foo<IntComparator>;
> The existing class declaration syntax does not allow the identifier > to be immediately followed by another identifier. Yes, your proposal is enough, but with equals sign it seems to be clearer that there is no new type defined (at least to me). It's just a compile time defined type alias for already existing type.
> Declaration scope > could be controlled by the same rules as for existing class declarations. These rules may probably be reused. However, a maximum scope available for alias should be likely a single compilation-unit. I'm afraid that wider scope is to complex to become worthy of implementing in the language. Without that needed, the compiler shall simply resolve each alias to the real type.
In any case, I'm another one who'd like to have the type aliasing feature available in Java.
piotr
Steve Wampler - 06 Dec 2007 17:12 GMT > How about a bit different syntax for /type aliasing/ (good term?): > [quoted text clipped - 6 lines] > interface IntComparator = Comparator<Integer>; > class FooInt = Foo<IntComparator>; I also like this, if it's added at all...
I have some concern about adding a new language construct simply because, well, it's adding a new language construct. It's much easier to add new features than it is to remove old ones, with the consequence that language complexity goes up.
In this particular case, many of the benefits (but not all, as others have pointed out!) of C's typedef can be provided already by subclassing. So is it worth adding the above for the net gain? Personally, I wouldn't do it but I'm certainly not going to lose any sleep if it is done - and would most likely use it, hypocrite that I am.
Incidently, I would argue that it would be better to do the above than to implement the equivalent using a preprocessor. The problem with a preprocessor approach is that the change is cosmetic and not carried into the runtime (hmmm, kinda like the current implementation of generics...) *or* the compilation. Error messages become more confusing, for example.
Using a preprocessor to *experiment* with a possible new language construct *is* fair game, however - to see if the perceived gains are worth it...
 Signature Steve Wampler -- swampler@noao.edu The gods that smiled on your birth are now laughing out loud.
Daniel Pitts - 05 Dec 2007 21:50 GMT > I don't think it's a debate for the best language, just that declaring > some variable using generics can be quite long. [quoted text clipped - 24 lines] > > Arnauld There is talk of using type inference to help reduce the verbosity of generics.
 Signature Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Stefan Ram - 06 Dec 2007 00:53 GMT >typedef Entity<Node, RendererType> nodeEntity; For Simplicity, I'll show how to do
typedef java.util.List<java.lang.String> StringList;
This can be done as follows:
class StringList extends java.util.ArrayList<java.lang.String>{}
public class Main { public static void main( final java.lang.String[] args ) { final StringList stringList = new StringList(); stringList.add( "string" ); /* stringList.add( java.lang.System.out ); */ /* error */ }}
I see that Steve Wampler posted the same idea while I was preparing this post.
Andreas Leitgeb - 06 Dec 2007 09:03 GMT >>typedef Entity<Node, RendererType> nodeEntity; > For Simplicity, I'll show how to do > typedef java.util.List<java.lang.String> StringList; > This can be done as follows: > class StringList extends java.util.ArrayList<java.lang.String>{} This, however will still fail, if I wanted to save into such a StringList-Variable an ArrayList<String> returned from foreign code.
Lew - 06 Dec 2007 13:12 GMT >>> typedef Entity<Node, RendererType> nodeEntity; >> For Simplicity, I'll show how to do [quoted text clipped - 5 lines] > such a StringList-Variable an ArrayList<String> returned > from foreign code. I guess I understand what people think typedef will do, but I just don't have such a hard time using List <String>.
And who uses fully-qualified names when they're griping about doing too much typing?
I really don't even have trouble with Entity< Node, Renderer > either. Or even Entity <Node, Renderer, Comparator< ? super Node >>. Or Map <Node, Integer>. To my eye the typedef would just hide the vital information revealed in the generics.
I would not favor adding "typedef" to the language. Just one more thing that hides what a type is, making you look elsewhere in the code to understand it, just because someone is too lazy to (let the IDE) type the generic types.
 Signature Lew
Andreas Leitgeb - 06 Dec 2007 13:56 GMT > I really don't even have trouble with Entity< Node, Renderer > either. Or > even Entity <Node, Renderer, Comparator< ? super Node >>. Or Map <Node, > Integer>. To my eye the typedef would just hide the vital information > revealed in the generics. As I said somewhere else already, I don't follow the logic of that argument, since giving names to classes or methods is just the same matter: you do not necessarily see the implementation at each use.
I do think, though, that "type inference" as mentioned elsewhere in the thread might just as well solve the problem at hand. (Not that a typedef didn't still have it's use.)
Arnauld.Loyer@gmail.com - 06 Dec 2007 14:10 GMT > I would not favor adding "typedef" to the language. Just one more thing that > hides what a type is, making you look elsewhere in the code to understand it, > just because someone is too lazy to (let the IDE) type the generic types. humpf...
Map<Foo<Comparator<Integer>>, Foo<Comparator<Integer>> is at least less readable than Map<FooInt, FooInt>, and it 's not about the easy typing my IDE provides.
And again, it's still a "simple" generic definition.
Creating a specific class is not a general solution and so, for me, a valid one. And furthermore, it strongly tides to a specific implementation (ArrayList in the StringList sample) or need to create a full delegation pattern to handle the interface. List has about 20 methods that must be implemented( oki oki my ide is smart and can generate it but) why should i have to create a two hundred lines class :s to deals with that
I would prefer an alias (some one talking about preprocessing huhhh :D) even it is scope dependant to prevent unknown or unwilling issue. What the point if outside your alias scope you get the fully qualified
Matt Humphrey - 06 Dec 2007 15:06 GMT >>>> typedef Entity<Node, RendererType> nodeEntity; >>> For Simplicity, I'll show how to do [quoted text clipped - 8 lines] > I guess I understand what people think typedef will do, but I just don't > have such a hard time using List <String>. Abstraction is needed when the type expressions themselves become complex:
Map <NameId,Map<NameId,List<PrimaryActionDefinition>>> allPads = new TreeMap <NameId,Map<NameId, List <PrimaryActionDefinition>>> ();
vs.
PrimaryActionTable = Map <NameId,Map<NameId,List<PrimaryActionDefinition>>; PrimaryActionTable pat = new TreeMap <PrimaryActionTable> ();
I don't want to define a new class because doing so will bind the generic expression to the base class. Using an interface is possible, but anonymous instantiation always produces an inner class and naming the class results in conflict between the unspecified generic type and the interface generic type.
This works: interface PrimaryActionTable extends Map<NameId,Map<NameId,List<ElementalModelActionDefinition>>> {}
But this does not: class PrimaryActionTableImpl extends TreeMap implements PrimaryActionTable { }
Cheers, Matt Humphrey http://www.iviz.com/
Tim Smith - 06 Dec 2007 02:53 GMT Some will consider this heresy, but there isn't actually any law against using a preprocessor with Java. :-)
Daniel Pitts - 06 Dec 2007 20:01 GMT > Some will consider this heresy, but there isn't actually any law against > using a preprocessor with Java. :-) No law, but no existing process either. Well, unless you count AspectJ or other AOP compilers. Or APT. Actually, maybe APT is what you want :-) Just, whatever you do, make sure that all the major IDE's can handle it, through plugins and whatnot.
 Signature Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Lew - 07 Dec 2007 01:58 GMT >> Some will consider this heresy, but there isn't actually any law against >> using a preprocessor with Java. :-) > No law, but no existing process either. Well, unless you count AspectJ > or other AOP compilers. Or APT. Actually, maybe APT is what you want :-) > Just, whatever you do, make sure that all the major IDE's can handle it, > through plugins and whatnot. Isn't AspectJ actually its own compiler, not a preprocessor?
It's also a separate language from Java.
 Signature Lew
Daniel Pitts - 07 Dec 2007 02:06 GMT >>> Some will consider this heresy, but there isn't actually any law against >>> using a preprocessor with Java. :-) [quoted text clipped - 6 lines] > > It's also a separate language from Java. My understanding is that its a language on-top of Java.
 Signature Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Lew - 07 Dec 2007 02:25 GMT >>>> Some will consider this heresy, but there isn't actually any law >>>> against [quoted text clipped - 9 lines] >> > My understanding is that its a language on-top of Java. Mine, too. It's that "on-top of" that I called "separate". (Java doesn't have "join points", for example.)
I am only familiar with what I've read on their site. Doesn't it use its own compiler?
 Signature Lew
Arne Vajhøj - 24 Dec 2007 23:12 GMT >>>>> Some will consider this heresy, but there isn't actually any law >>>>> against [quoted text clipped - 15 lines] > I am only familiar with what I've read on their site. Doesn't it use > its own compiler? It does.
But considering that Java source code and Java byte code is very 1:1 then the difference is not big.
Arne
Roedy Green - 06 Dec 2007 09:30 GMT On Wed, 5 Dec 2007 08:27:18 -0800 (PST), "Robert M. Gary" <N7093v@gmail.com> wrote, quoted or indirectly quoted someone who said
>I use Generic's a lot and >often find that declaring an iterator for a Generic can take two lines >of code. why? See http://mindprod.com/jgloss/unmain.html
It is a feature that presents a dangerously strong temptation to write unmaintainable, inscrutable code.
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Wojtek - 06 Dec 2007 18:23 GMT Roedy Green wrote :
> On Wed, 5 Dec 2007 08:27:18 -0800 (PST), "Robert M. Gary" > <N7093v@gmail.com> wrote, quoted or indirectly quoted someone who said [quoted text clipped - 7 lines] > It is a feature that presents a dangerously strong temptation to write > unmaintainable, inscrutable code. This.
 Signature Wojtek :-)
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|