Groups | Search | Server Info | Keyboard shortcuts | Login | Register
Groups > comp.lang.java.programmer > #4587
| From | Susan Calvin <s.calvin@usr.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Java generics and type erasure |
| Date | 2011-05-26 01:12 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <irk9dr$j5p$4@dont-email.me> (permalink) |
| References | (3 earlier) <Xns9EEF8515AF3B6vaj4088ianshef@138.125.254.103> <irh6kb$gse$1@news.albasani.net> <Xns9EEFA3AE378DDvaj4088ianshef@138.125.254.103> <irhhik$9rc$3@dont-email.me> <irjv7c$iv0$5@lust.ihug.co.nz> |
On Thu, 26 May 2011 10:18:20 +1200, Lawrence D'Oliveiro wrote:
> In message <irhhik$9rc$3@dont-email.me>, Susan Calvin wrote:
>
>> The only reason I can think of for not doing this (the logic seems
>> simple enough to implement) is that it turned out doing so would break
>> legacy code that used util collections' raw types. Was that why?
>
> Yup. Much of the complexity attendant on introducing generics into Java
> was precisely because of that need for backward compatibility.
>
>> Perhaps there should be a compile flag that turns on the
>> legacy-compatible behavior for use when compiling 1.4 and older
>> sources, but which is off by default?
>
> What happens when you mix code compiled with that flag, with code that
> was compiled without?
Why, nothing, of course, since generics don't exist at run-time. Both
Integer x = aFoo.m1.get("quux");
(in the file compiled with the flag) and
Integer x = (Integer)(aFoo.m1.get("quux"));
(in the file compiled without it) would compile to the same bytecode,
including a checkcast for Integer.
This is only an issue for source compatibility, not binary compatibility.
And there are far worse problems with generics and especially with
autoboxing. For example:
int x = aFoo.m1.get("quux");
Guess what happens if "quux" is not found? There should probably be a
shorthand way to check for this -- maybe a variation on the ?: operator
that tests its left hand side for null, evaluates to it if it's not, and
evaluates to its right hand side otherwise, e.g.
int x = aFoo.m1.get("quux")?:-1
perhaps would make x -1 as a sentinel for "not found" in this instance.
Actually, the Java 5 features have several rough spots that aren't easy
to smooth over. Java really should have been designed with generics and
better integration of primitive types into the type system from the
beginning. Now we have these ad hoc bandaid solutions that show visible
seams here and there and we're probably going to have to live with them
for the next forty years, just as we're living now with 40-year-old COBOL
code.
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
Java generics and type erasure Marcin Pietraszek <m.pietraszek@gmail.com> - 2011-05-23 13:18 -0700
Re: Java generics and type erasure "John B. Matthews" <nospam@nospam.invalid> - 2011-05-23 22:54 -0400
Re: Java generics and type erasure Lew <noone@lewscanon.com> - 2011-05-24 01:45 -0400
Re: Java generics and type erasure Marcin Pietraszek <m.pietraszek@gmail.com> - 2011-05-24 12:39 -0700
Re: Java generics and type erasure Ian Shef <invalid@avoiding.spam> - 2011-05-24 20:04 +0000
Re: Java generics and type erasure Lew <noone@lewscanon.com> - 2011-05-24 17:06 -0400
Re: Java generics and type erasure Ian Shef <invalid@avoiding.spam> - 2011-05-24 23:05 +0000
Re: Java generics and type erasure Susan Calvin <s.calvin@usr.invalid> - 2011-05-25 00:13 +0000
Re: Java generics and type erasure Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-05-26 10:18 +1200
Re: Java generics and type erasure Susan Calvin <s.calvin@usr.invalid> - 2011-05-26 01:12 +0000
Re: Java generics and type erasure Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-05-26 13:59 +1200
Re: Java generics and type erasure Susan Calvin <s.calvin@usr.invalid> - 2011-05-26 04:18 +0000
Re: Java generics and type erasure Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-05-26 18:48 +1200
Re: Java generics and type erasure Esmond Pitt <esmond.pitt@bigpond.com> - 2011-05-26 18:34 +1000
Re: Java generics and type erasure Lew <noone@lewscanon.com> - 2011-05-26 07:45 -0400
Re: Java generics and type erasure "John B. Matthews" <nospam@nospam.invalid> - 2011-05-26 15:25 -0400
Re: Java generics and type erasure Esmond Pitt <esmond.pitt@bigpond.com> - 2011-05-27 12:01 +1000
Re: Java generics and type erasure Lew <noone@lewscanon.com> - 2011-05-26 22:15 -0400
Re: Java generics and type erasure Esmond Pitt <esmond.pitt@bigpond.com> - 2011-05-27 13:04 +1000
Re: Java generics and type erasure Lew <noone@lewscanon.com> - 2011-05-27 01:27 -0400
Re: Java generics and type erasure Esmond Pitt <esmond.pitt@bigpond.com> - 2011-05-27 18:07 +1000
Re: Java generics and type erasure Lew <noone@lewscanon.com> - 2011-05-27 10:18 -0400
Re: Java generics and type erasure Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-05-27 17:03 +0000
Re: Java generics and type erasure Lew <noone@lewscanon.com> - 2011-05-27 13:27 -0400
Re: Java generics and type erasure Esmond Pitt <esmond.pitt@bigpond.com> - 2011-05-30 15:19 +1000
Re: Java generics and type erasure Tom McGlynn <taqmcg@gmail.com> - 2011-05-26 19:53 -0700
Re: Java generics and type erasure Lew <noone@lewscanon.com> - 2011-05-27 01:30 -0400
Re: Java generics and type erasure Esmond Pitt <esmond.pitt@bigpond.com> - 2011-05-27 18:09 +1000
Re: Java generics and type erasure Lew <noone@lewscanon.com> - 2011-05-27 10:22 -0400
Re: Java generics and type erasure Esmond Pitt <esmond.pitt@bigpond.com> - 2011-05-30 15:23 +1000
Re: Java generics and type erasure Lew <noone@lewscanon.com> - 2011-05-30 02:35 -0400
csiph-web