On Tue, Aug 01, 2000 at 10:09:32AM -0700, Tony Payne wrote:
> The ability to have strong-typing (I don't trust Junior Engineers to get it
> right and I don't have time to check every line of code they write)

Several people have suggested strong typing as a feature, and have been shot
down one by one.  However, I think it can be done without forcing it on
everyone.

In fact, it can be done with Perl 5, as various people have pointed out,
through tying.  Unfortunately, accessing and manipulating tied variables is
incredibly slow.  Improving their speed is obviously an implementation
detail (not belonging to a -language list), but would improve the language
by allowing the addition of extra syntax, like type checking, without
requiring it to be in core (that is, perl core, not the core distribution).

So, I envision syntax like this:

    use typing;  # place your fingers on the home row..

    integer $foo, $bar, $blah;
    string($baz, $qux) = ("bazarific", "quxaroni");

    # and even..

    my integer $quux = 4;


Except for the last part, the above syntax is doable in Perl 5.  At least, I
assume the string example is; I couldn't quite get lvalue subs to work as I
expected when testing things.

With tying, however, exceptions (That's not an integer! *croak*) are made at
run-time, rather than compile-time (as would be more useful).  So you have
to test all code paths if you are to use this as any sort of internal sanity
checking; I assume most everyone does this, but it would be useful for it to
be a compile-time check, so that perl -c would catch all of your errors. 
This could possibly be accomplished by allowing arguments to the pragma, or
variable initialization:

    use typing qw(very-strict);

    my integer $foo : very-strict = 4;

Which would enforce that you can only assign integer constants to $foo
(which are seen at compile-time), or other similarly declared integers (or
possibly promoted floats, chars, etc. if you wanted to get C-like).  This,
then, could allow you to do checking at compile-time.

Which then raises a few more problems (whew): how do you coax user input
(which is an SV) into a value $foo can accept with very-strict on?; what
happens when an external function (say, from a module) is being very-strict,
but is passed arguments from code that doesn't do type checking?


Now, once you have that, you could then extend the syntax (provided we have
a way of doing tie-like functions (where the first argument is any data
type) in user-space):

    integer @ints = (1 .. 20);

Which is what someone out there was suggesting; this would give you an array
optimized to hold only integers, rather than SVs.


So, strong typing is doable, and not burdensome to those who don't want to
use it (which includes me).  You can even get some efficiency gains from it.

This is not necessarily an endorsement for type checking, but I see more
than a few people asking for it, and if it can be done without severely
impacting those who don't want it, there's little reason to deny them (that
I can see).


Michael
--
Administrator                      www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

Reply via email to