On Friday, November 1, 2002, at 01:38  PM, David Whipp wrote:
Presumably, there exist rules for implicit casting when
comparing objects of different types. If we have a rule
My initial assumption is that nothing would change. Namely, == compares numerically, eq compares strings, and '?' enforces booleanness. In numeric context, bool evaluates to 1 or 0; in string context, "1" or "0":

my bool $t = 1;

($t == 1) true
($t == 5) false
($t == ?5) true

($t eq '') false
($t eq '0') false
($t eq '1') true
($t eq 'foo') false
($t eq ?'foo') true

.... which is the exact equiv of "bit" behavior, I believe. It's just a more descriptive name for a common usage.

But here's where things get more interesting. If say $t is a numeric "bit", not a true boolean:

($t == (0 but true )) (1 != 0, so false) ***
($t == (1 but false)) (1 == 1, so true ) ***
($t == ?(0 but true )) (1 == 1, so true )
($t == ?(1 but false)) (1 != 0, so false)

Which may or may not be surprising. But if you say you can store true booleans as a "bool" type, and '==' knows how to deal with them:

($t == (0 but true )) (true == true, so true ) ***
($t == (1 but false)) (true != false, so false) ***
($t == ?(0 but true )) (true == true, so true )
($t == ?(1 but false)) (true != false, so false)

Whether that is an argument for or against a non-numeric 'bool' type depends on your point of view. In any event, if I want to store the result of a boolean expression:

my $lit = ($lights == "on");

What type do I declare $lit as, in order for it to do what I mean?

MikeL



Reply via email to