Re: No coercion in for-each loop?
Roedy Green wrote:
On Sun, 28 Oct 2007 09:10:51 -0700, "Mike Schilling"
<mscottschilling@hotmail.com> wrote, quoted or indirectly quoted
someone who said :
Object o;
String s = o;
won't compile. Why would you expect different behavior in a for
loop?
1. Except to the extent that generics are implemented by doing so.
2. autoboxing-unboxing which is can't be done with a cast, but is in
the same spirit.
One of my early flames at Java was the lack of orthogonality in
conversions. I figured if you wanted to specially mark them, either
they should all be done with (convert) or with (anytype).
After experiences with PL/1 which will automatically provide a
conversion from pretty well anything to anything without comment using
the most improbable and practically useless algorithm, I am a firm
believer in explicitly marking conversions.
Oh yes. In fact, PL/I proved (by trying and failing miserably) that even
among the usual kind of numeric types, there's no way to construct a system
of conversions that's internally consistent and whose results are always
intuitive. Even a type system as simple as XPath 1.0's show this. It has
three scalar types (plus a non-scalar type that's a set of XML nodes.)
1. Numeric (double)
2. Boolean (true/false)
3. String (Unicode characters; can be empty but never null.)
Each has a standard value to which an empty node set automatically
converts:
1. 0.0
2. false
3 the empty string
Conversions among scalar types *almost* work:
1. 0.0 -> false, 0.0 -> empty string
2. false -> 0.0, false -> "false"
3. empty string -> 0.0, empty string -> false
The problem is that the string "false" causes an exception when converted to
a number, and becomes true when converted to a boolean (because it's
non-empty.)