develooper Front page | perl.perl6.internals | Postings from December 2004

Re: Parrot & Strong typing

Thread Previous | Thread Next
From:
Dan Sugalski
Date:
December 1, 2004 08:05
Subject:
Re: Parrot & Strong typing
Message ID:
a0620073dbdd392edf0fb@[172.24.18.155]
At 4:53 PM +1000 12/1/04, Cameron Zemek wrote:

[Yeah, I snipped the first question. It's early, and I've not had 
enough coffee :)]

>Also could the Parrot VM be used effectively with strong typing languages.

Absolutely. At least some of the languages we're interested in, 
specifically perl 5 and perl 6, (I'm less sure about python, ruby, 
and tcl, though I'm pretty sure they are also strongly typed, at 
least in some circumstances) are very strongly typed.

Parrot, on the other hand, is not going to be well-suited at all to 
*static* typing, though since that's generally more a compiler thing 
than a runtime thing it's less of an issue. (Though if you're going 
to play well with others and the library it may be a bit of a 
headache, since many of the types will be "I dunno", and we're likely 
not going to do much to keep code from swapping in  subs and methods 
at runtime with prototypes and type guarantees that are different 
from what they're replacing)

For those folks playing along at home, I'll take a bit to talk about 
the difference between strong/weak typing and static/dynamic typing. 
(I think I blathered on about this on my blog, but I can't be 
bothered to go look it up right now :)

A *strong* type system is one that doesn't allow you to violate the 
constraints you put on variables, while a *weak* type system does 
allow you to do so -- it is, basically, a measure of how badly you 
can lie to the compiler about the type of a variable. This is a 
continuum, so you'll find languages are strong-ish or weak-ish.

C, for example, is weakly typed. That is, while you tell the system 
that a variable is one thing or another (an int, or a float), you're 
perfectly welcome to treat it as another type. This is *especially* 
true of values you get to via pointers. For example, this snippet 
(and yes, it's a bit more explicit than it needs to be. Cope, you 
pedants :):

     char foo[4] = "abcd";
     printf("%i", *(int *)&foo[0]);

tells the C compiler that foo is a 4 character string with the value 
"abcd", but in the next statement we get a pointer to the start of 
the string, tell the compiler "No, really, this is a pointer to an 
int. Really!" and then dereference it as if the string "abcd" really 
*was* an integer. If C were strongly typed you couldn't do that.

Perl, on the other hand, is strongly typed. If you try this:

     $foo = "abcd";
     $bar = \$foo; # Get a reference to $foo
     print $bar->[10]; # Treat $bar as if it were a reference to an array

Perl will yell at you, telling you that $bar isn't an array 
reference. If perl were weakly typed (like C is) it'd let you, but it 
doesn't.

And yeah, I'm throwing perl in here because it has what is reasonably 
considered a bizarre type system (it doesn't have integers or strings 
as types. It has "singular thing", "aggregate thing accessed via 
integer offset", and "aggregate thing accessed by name" as types, 
with a lot of autoconversion and context sensitive behaviour to give 
people's brains a twist) but it's still a strong one -- you're just 
not allowed to violate it.

Static vs dynamic typing, on the other hand, refers to how much 
knowledge you have at compile time about the types of your variables. 
For example, with Ruby (since I have a manual handy and wouldn't want 
to embarrass myself in python with Sam around :) code like:

     s = object.bar(1,2,3)

is just fine -- this is the first time we use s, it's never been 
declared, and the compiler may well have no clue as to what 
object.bar returns (heck, it may not even exist at compiletime). We 
can only know the type of s at a point in time. C, on the other hand, 
is statically typed -- you must give the compiler a type for each 
variable. (Even if you lie about it later)

Strong/weak and static/dynamic typing can mix just fine. You can have 
a strong dynamically typed language (this'd be one where you don't 
know the type of a variable at compiletime, but once it gets a type 
it *keeps* that type) or a weakly statically typed language, like C, 
where you *must* give types at compile time to everything but can 
like left and right about it at runtime.

(And thus endeth the rant :)

Anyway, Parrot'll do strong typing if you want it to, no big deal. 
PMCs are in complete control on assignment, so you can have all the 
strong types check to see what they're handed and pitch a fit at 
runtime if it's wrong.
-- 
				Dan

--------------------------------------it's like this-------------------
Dan Sugalski                          even samurai
dan@sidhe.org                         have teddy bears and even
                                       teddy bears get drunk

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About