> Mailing-List: contact [EMAIL PROTECTED]; run by ezmlm
> Date: Thu, 31 Oct 2002 22:36:58 -0800
> From: Michael Lazzaro <[EMAIL PROTECTED]>
> X-Accept-Language: en,pdf
> Cc: [EMAIL PROTECTED]
> X-SMTPD: qpsmtpd/0.12, http://develooper.com/code/qpsmtpd/
> 
> Larry Wall wrote:
>  > Please don't think of C<bit> as a boolean type. There is no boolean
>  > type in Perl, only a boolean context.  Or looking at it from the
>  > other direction, *every* type is a boolean type.
> 
> Er... dang.  While I certainly agree with the concept in principle, I'm 
> having a hard time sticking to it when I'm writing beginner-level docs. 
>   Suppose I'm declaring something like:
> 
>      class Appliance {
> 
>          has bit $.is_plugged_in;
>          has bit $.is_turned_on;
>          has bit $.has_ul_approval;
>      }
> 
> I'm declaring three attributes that are specifically always either true 
> or false.  They can *only* be true/false, or yes/no, or 1/0 -- they have 
> no other possible values, and I want to declare them in such a way as to 
> clearly enforce that behavior.  C<$.is_plugged_in> is never "5" or 
> "timmy" -- only true or false, period!  By definition, my _intention_ is 
> therefore to declare these three attributes as containing "boolean" values.

I suppose, since anything can have the "true" or "false" properties,
you could roll your own boolean type like so:

    class bool { }

Then

    my bool $electricity;

Can contain nothing at all.  But it can be true or false, since
"everything is a boolean."  I like that idea.  You would use it (a tad
awkwardly) as such:

    $electricity but= true  if $plug;
    $electricity but= false if $short_circuit;

Luke

Reply via email to