[Python-ideas] [Wild Idea] Static Ducks

Mathias Panzenböck grosser.meister.morti at gmx.net
Tue Sep 22 05:05:41 CEST 2009


On 09/22/2009 01:18 AM, 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
> 

Written from memory, C++:

#include <string>
#include <cstdio>

int operator + (const std::string& lhs, int rhs) {
	int i = 0;
	std::sscanf(lhs.c_str(), "%d", &i);
	return i + rhs;
}

int main() {
	std::string s = "2";
	std::printf("%d\n", s + 2);
	return 0;
}

Prints:
4

I don't think this is a valid test to determine how a language is typed. Ok, C++
is more or less weakly typed (for other reasons), but I think you could write
something similar in C#, too. And C# is strongly typed.

> 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.
> 
> 
> 




More information about the Python-ideas mailing list