Re: No coercion in for-each loop?



Roedy Green wrote:
On Sun, 28 Oct 2007 09:10:51 -0700, "Mike Schilling"
<mscottschilling@xxxxxxxxxxx> 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.)


.



Relevant Pages

  • Re: if question
    ... > (which is trivial if it's already a boolean). ... > the boolean value true (the empty string converts to false, ... > performed before comparing. ... conversions in COMPARISONS. ...
    (microsoft.public.scripting.jscript)
  • Re: Ada.Strings.Bounded
    ... for Unbounded_String'Conversion use To_String; ... Below you immediately spot the problem of conversions ... String'Class and you wanted to inherit everything form String. ... The implementation of inheritance (by the compiler) will require you to ...
    (comp.lang.ada)
  • Re: Fastest Date Sort
    ... So the whole program is done with String ... >>> But what kind of trouble could I be asking for? ... > default type conversions and formatting, ... and are best avoided by using explicit conversions. ...
    (comp.lang.basic.visual.misc)
  • Re: one-liner for characater replacement
    ... that whole can of worms about internal representations of LOGICALs ... Since a string is almost identical to a rank-1 ... No matter what INTEGER KIND is the ... So our hypothetical conversions between LOGICAL ...
    (comp.lang.fortran)
  • Re: Open Sound Control
    ... question of time granularity (many small packets in short sequence), ... I think it is too many small conversions within 1 packet, ... string conversion is done until you explicitly ask for the string ... single call (with a pattern of more than one scalar type ...
    (comp.lang.tcl)