Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Static checking

by fletcher_the_dog (Friar)
on Nov 05, 2003 at 16:34 UTC ( [id://304767]=note: print w/replies, xml ) Need Help??


in reply to Static checking

I may be totally wrong, but I always thought that the advantage of static typing had mostly to do with speed and size. For example in the following sub, every time it runs it has to first look up what type of object $thing is and then check to see if that type of object has a "to_xml" method.
sub foo{ my $thing = shift; print $thing->to_xml; }
But in the following (probably bad) C++ code, the function can be looked up just once at compile time, because the function knows what type of object 'thing' will be:
void foo(bar thing){ cout << thing.to_xml(); }
Yet another layer is added when you use overloading. Let's say that a variable is used in a concatenation, perl must first determine what if any class of object that variable belongs to. If the variable does belong to a class it must determine if that class has the concatentation operator overloaded, if it does then in must determine if the subroutine exists that it is overloaded with, and then it calls that subroutine. In a static typed language this only has to be done once at compile time.
The size difference shows most with ints and chars. As I understand it in perl every scalar is a C struct. If a variable is only ever going to be an int, then it is a waste to make a struct for it.
I am not saying that static typing is better, but I think there are some speed and size advantages.

Replies are listed 'Best First'.
Re: Re: Static checking
by hardburn (Abbot) on Nov 05, 2003 at 17:17 UTC

    I may be totally wrong, but I always thought that the advantage of static typing had mostly to do with speed and size.

    Those are often advantages to a type system, but it's also a debugging aid. In your Perl example, what happens if $thing is a simple scalar instead of a blessed reference? Or what if it is an object, but that object doesn't implement a to_xml method? In Perl5, you won't know these things until runtime. Whereas with the C++ example, the compiler will complain if thing isn't a bar instance.

    Though the C/C++ type system is broken in some ways which hurt debugging, which is covered in Dominus' Strong Typing talk (noted earlier in this thread).

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://304767]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 20:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found