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.
The problem I'm having is that there are few good ways to describe the
above behavior without using the word "boolean". The word is
conspicuous in its absence; it's like someone trying to describe Daffy
Duck to you without using the word "duck". Perhaps boolean is spelled
bit (or type bool ::= bit), but the associated value is unambiguously
"boolean", which makes the attribute logically "boolean" in type.
Similarly I would assume that we want the capability to refer to boolean
context by name (just as we can refer to scalar, list (array?), numeric,
string, and other contexts) if for no other reason than to be able to
make declarations like:
sub foo (int $i; str $s; bool $y) { ... }
which, from appearances, seems to strongly _imply_ to the user that
there is indeed a 'bool' type, just as there is an 'int' type and a
'str' type. (They may be wrong, but that's intuitively what the above
signature _appears_ to say.)
So while I understand the philosophical/semantic reasons for the absence
of a true boolean type, I wonder how easy it will be to describe the
principle to newcomers, and if it's worth it. When someone asks "what's
the boolean type in Perl?" I'd rather answer "bit" than "Perl doesn't
have one", if for no other reason than the latter answer will completely
freak them out. :-)
Feedback? What am I missing?
MikeL
- Primitive Boolean type? Michael Lazzaro
- Re: Primitive Boolean type? David Wheeler
- Re: Primitive Boolean type? Larry Wall
- Re: Primitive Boolean type? Larry Wall
- Re: Primitive Boolean type? Michael Lazzaro
- Re: Primitive Boolean type? Luke Palmer
- Re: Primitive Boolean type? David Wheeler
- Re: Primitive Boolean type? Mark J. Reed
- Re: Primitive Boolean type? Michael Lazzaro
- Re: Primitive Boolean type? David Wheeler
- Re: Primitive Boolean type? Austin Hastings
- Re: Primitive Boolean type? Larry Wall
- Re: Primitive Boolean type? Damian Conway
- Re: Primitive Boolean type? Michael Lazzaro
- Re: Primitive Boolean type? Smylers