Weakly Typed

Disagreeing with the former definition below: a WeaklyTyped language or language construct is one that allows many operations without regard for whether they are defined for the types of the operands in question. The extreme of this is untyped languages, such as assembly language and BCPL, in which the only type is the machine word (also called MonoType?).

This contrasts with StronglyTyped languages, in which almost all operations are checked (either statically or at run-time) for type correctness, with the exception of only a small number of loopholes/escapes from typesafety. They are usually TypeSafe -- unless the loopholes are used.

See also StronglyTypedWithoutLoopholes.

For instance Modula 3 is StronglyTyped, but not StronglyTypedWithoutLoopholes because it allows the "unsafe" qualifier to force operations to be accepted that cannot be verified to be type safe. ANSI C is StronglyTyped, but not StronglyTypedWithoutLoopholes because it allows a small number of type-unsafe operations, such as casting any pointer type to any other pointer type.

The number of directly type-unsafe operations in ANSI C is not small, and it has many causes of UndefinedBehavior that also defeat type safety. The typesafe subset of the language is severely limited in expressiveness.

C is less StronglyTyped than Modula 3 because it lacks some safety mechanisms altogether, e.g. array bounds checking. More accurately, C is fairly strongly statically typed, but lacks dynamic type checks of any sort.


A WeaklyTyped programming construct gets around the type system in your program. In other words, type failures are possible and cause UndefinedBehavior -- neither the compiler nor the runtime traps and rejects these errors. Contrast with StronglyTyped.

Examples:

Some languages more or less force use of WeaklyTyped constructs upon one. C is a good example: to store pointers in a collection, you have to cast them to void* (losing all type information) and cast them back again on retrieval.

Others make it available, but you don't have to use it, and generally shouldn't unless you have a really good reason. C++ is a good example: times when you actually need reinterpret_cast<> are few and far between.

Languages where WeaklyTyped constructs are accepted as idiomatic programming practice are often called WeaklyTyped themselves, although it's still possible to write programs in them that aren't WeaklyTyped at all, e.g. this C program that returns double the number of arguments it's passed:

 int main(int argc, char *argv[], char *envp[]) {
int n = argc * 2;
return n;
 }


I've also seen WeaklyTyped used for languages which are typesafe, but contain/allow a large number of implicit type conversions--i.e. a language such as JavaScript which allows operations such as "2" * "3" (which gives you 6; as JavaScript doesn't know how to multiply strings, it converts 'em to integers and multiplies those. Incidentally, "2" + "3" isn't 5 in JavaScript, it's "23"--the + operator is defined for strings, and is concatenation and not addition).

In other words, a bit of SyntacticSugar (one which is useful in small programs, but more likely to cause problems in large programs as unexpected type conversions cause weird and bizarre problems--as this can easily turn an incorrect program that failes to compile or halts at runtime into a different incorrect program which dies at an unrelated places, or (worse) terminates normally but produces incorrect results).

C++ can be made weakly typed (under this definition), as it is quite easy to provide a nest of implicit type conversions, both into a given type (through overloading the assignment operator and the CastConstructor), and out of a given type (via OperatorTee?).


See http://www.lisp-p.org/tat/


The advantages of untyped or weakly typed languages over strongly typed languages is often touted as something a RealProgrammer can take advantage of. The details are rarely forthcoming in the articles I've read. As a user of BorlandDelphi (based on strongly typed Pascal), can someone tell me a real-world example of how weak typing could be useful? --JosephStyons


perl would be an great example of a weakly typed languige

print "Who is the richest man";
chomp($ans = <>);
if($ans=="Bill Gates"){ # wrong
print "Correct"
} else{
print "Incorrect"
}
this is an type error because the answer is converted into an number 0 but if you use 'eq' then it will compare them like strings


dinamic typeing is useful but not weak typeing.


See also: WeakTypesLie


CategoryLanguageTyping, CategoryTypingDebate


EditText of this page (last edited May 31, 2012) or FindPage with title or text search