On Wed, Feb 16, 2005 at 02:29:36PM +0800, Autrijus Tang wrote:
: Just a quick question. The prettyprinter of Pugs (the thing that
: handles the ".perl" method) currently prints out boolean true and
: false as #t and #f, which is obviously not correct.
: : pugs> (1 > 2, 2 > 1)
: (#f, #t)
: : What should I do, though? Inventing two primitives, "true" and
: "false"? Or is there a way to annotate values with types, similar
: to Haskell's "::" construct?
: : pugs> (1 > 2, 2 > 1)
: (0 as Bool, 1 as Bool)
The latest S12 has it as bool::true and bool::false. (In general, you can print out any enum as "role::identifier".) When unambiguous, bare "true" and "false" can be used, but in any event the use of symbolic booleans is somewhat discouraged in Perl because it leads people to write stupidities like
while foo() == true {...}
Instead, we try to make sure all types know how to behave in boolean context directly. Any object that does the "bool" role has an attribute of type "bit", which will return 0 if the object is false, and 1 if the object is true. (The optimizer is free to optimize away the method call if it can, of course. Native integers and floats are always true if non-zero, for instance. But object types can always be derived from, or have "but true" mixed into them.)
We already have +^ ~^ ?^ +| ~| ?| etc..
Why not allow data-type prefix for the comparison operators also, so we'd get, to mention a few, ~== (same as 'eq') ~< (same as 'lt') ~<= (same as 'le') - and of course boolean versions ?== and ?!= (The others really don't have use with just 2 possible values.)
Then a programmer could write
while foo() ?== true {...}
and it would be ok. After all, perl is all about giving more than one way to do it.
-- Markus Laire <Jam. 1:5-6>