strong/weak typing and pointers

Steven Bethard steven.bethard at gmail.com
Tue Nov 2 16:39:19 EST 2004


Gabriel Zachmann <zach <at> cs.uni-bonn.de> writes:
> 
> >  (1) Weakly-typed languages allow you to take a block of memory that was
> >  originally defined as one type and reinterpret the bits of this block as 
> >  another type[1]. (This is the definition usually used in Programming 
> >  Languages literature.)
> >  
> >  (2) Weakly-typed languages have more implicit coercions than strongly-typed
> >  languages.  (This seems to be the favored definition on this newsgroup.)
> 
> Is either of them a subset of the other, generally speaking?

Not really -- they're pretty much orthogonal.

For example, C is weakly-typed (PL theory definition) but has only a few
implicit coercions (e.g. int->float with the + operator).  Of course, C only has
a very few basic types so it doesn't have as many chances to support implicit
coercions.  For example, there is no string type in C (only arrays of
characters), so it wouldn't really make sense to talk about some sort of
string->int coercion.

Python is strongly-typed (PL theory definition) and also has only a few implicit
coercions (e.g. int->float with the + operator).

ML is strongly-typed (PL theory definition) and has *very* few (perhaps none?)
implicit coercions, e.g.:

  # 1 + 1.0;;  
    
  Characters 4-7:
    1 + 1.0;;  
        ^^^
  This expression has type float but is here used with type int

not even the int->float conversion common to C, Java, Python, etc. is supported.

I don't know enough about Pascal, Perl or PHP to tell whether they are
weakly-typed or strongly-typed (PL theory definition).  Taking advantage of weak
typing isn't something I do much, so even in languages that I have passing
familiarity with, I've generally used only the strongly typed features.  To
someone who knows about Pascal, Perl or PHP: Can you reinterpret an object's
memory block as a different object like you can in C?

I've never had to code in Pascal, but my understanding was that there weren't
too many implicit coercions...  Please correct me on this one if anyone knows
better!  Examples in this thread suggest that both Perl and PHP have large
numbers of implicit coercions.


> both, if you don't mind 

I'm not willing to commit to anything for languages that I'm not really familiar
with, but here's what I'd say:

>From weakly-typed to strongly-typed (PL theory definition):

C < Java, Python, ML

Basically, there's no hierarchy, just weakly-typed and strongly-typed.  You
might get a hierarchy if you had some languages that allowed only some (but not
all) objects to be reinterpreted.

>From many implicit coercions to few implicit coercions:

C, Java, Python < ML

Of course, this isn't really helpful because C, Java and Python all contain only
a few implicit coercions (e.g. int->float with the + operator).  You'll have to
get a PHP/Perl/Pascal expert in to make any claims about them.

Steve




More information about the Python-list mailing list