[Python-ideas] [Wild Idea] Static Ducks

MRAB python at mrabarnett.plus.com
Tue Sep 22 01:57:47 CEST 2009


Steven D'Aprano wrote:
> On Mon, 21 Sep 2009 11:55:57 pm MRAB wrote:
>> I know of one language which is weakly typed: BCPL. It's only type is
>> the bit pattern. It's the function or operator which interprets the
>> bits as representing a certain value.
> 
> That would make BCPL an untyped language, like assembly. If the language 
> has no types, it can't be either weakly or strongly typed.
> 
> Weak and string typing is a matter of degree. Most languages are weakly 
> typed to some extent, for instance Python will automatically coerce 
> various number types so you can add integers to floats etc., as will 
> Pascal. Numeric coercion is so widespread that most people consider it 
> an exception to weak typing: if all the language coerces are numeric 
> types, then it's still strongly typed.
> 
> The classic test for weak typing versus strong typing is operations on 
> mixed integers and strings. Can you add or concatenate strings to 
> integers without an explicit conversion? Perl is weakly typed:
> 
> $ perl -e 'print "2"+2; print "\n";'
> 4
> $ perl -e 'print "2".2; print "\n";'
> 22
> 
> PHP and (I think) Javascript will do the same.
> 
> It's been some years since I've used it, but I recall Apple's Hypertalk 
> behaved similarly. I think you could say 2&"2" (returns "22") and 2+"2" 
> (returns 4). Hypertalk is no longer supported, but I expect Apple's 
> current generation scripting language, AppleScript, would probably be 
> the same.
> 
I read that someone re-wrote a Perl script in Python and found that a
process that it called sometimes returned "ERROR" instead of a numeric
string, which Perl would just coerce to 0, thus hiding the error!

The Icon language also performs automatic coercion, but invalid strings
cause a runtime error (unfortunately the language doesn't support
catchable exceptions, or at least didn't in the last version I know of).
Icon also shows the disadvantage of automatic coercions: there's an
operator for addition, one for string concatenation, and one for list
concatenation. If you wanted to add sets (not just charsets), you'd need
yet another operator. In Python you can just reuse them and let the
classes decide what they do! :-)



More information about the Python-ideas mailing list