Phoenix.pm: typesafety.pm - meeting topic?
scott at illogics.org
Tue Aug 5 04:37:56 CDT 2003
I've just unleashed typesafety.pm on CPAN. Okey, I did it a week ago. Or so.
What does it do?
It lets you declare the type of object that a scalar will hold, and the
return values of methods, and the arguments accepted by methods, and
performs an inspection (or static analisys) of your program to make sure
that types are used consistently. There are a whole lot fo reasons you'd
want to do this, and "real" OO languages (like Java, C++, Simula, Smalltalk)
always do. Even some non-OO languages (ML, eg OcaML) do.
In a nutshell, OO types are like sets, and each method accepts things
of a type that belong to a certain set (Animals, Pets, Cats, Siamese Cats, ...).
Things belong to sets that belong to other sets, and things may also belong
to multiple completely different sets. By formalizing what types are
accepted, returned, and stored, not only are stupid mistakes found at
"compile time", but you are forced to think about methods in terms of
what situations they will be required to deal with. Sometimes a method is
too specific - it should be working on Pets instead of Cats. Sometimes
a method is not specific enough - a bathe() method shouldn't accept
just any Pet, or it might get a Parakeet or a Cat. It should only accept
Dogs, or it should only accept things that are of type Washable, which
might include many things that are Pets and things that are of type
Dishwear and Clothing. Oh, and using strict types also means you don't
have to explicitly check the types of incoming objects to make sure
that they are what you expect. Code tends to "grow" a lot of checks
as it is debugged, and it clutters things up and slows things down.
I wrote up a Wiki pages on the subject:
Mark Jason Dominus also did an excellent presentation on the topic:
Of course, that predates typesafety.pm.
If there is interest, I'll do a proper presentation on this subject:
1. Why use strict types
2. Strict types in Java - declarations
3. Strict types in Perl - declarations, error messages, what is checked
4. Static code analysis using the B:: backend and other insanity =)
More information about the Phoenix-pm