Weak typing

From Wikipedia, the free encyclopedia

In computer science, weak typing is a property attributed to the type systems of some programming languages. It is the opposite of strong typing, and consequently the term weak typing has as many different meanings as strong typing does (see strong typing for a list and detailed discussion). One of the more common definitions states that weakly-typed programming languages are those that support either implicit type conversion, ad-hoc polymorphism (also known as overloading) or both. This can give programmers the impression that strict adherence to typing rules is less important than in strongly-typed languages and hence that the type system is "weaker". However, such languages usually have restrictions on what programmers can do with values of a given type; thus it is possible for a weakly-typed language to be type safe. Moreover, weakly-typed languages may be statically typed, in which case overloading is resolved statically and type conversion operations are inserted by the compiler, or dynamically typed, in which case everything is resolved at run time.

Weak typing requires less effort of the programmer than strong typing, because the compiler or interpreter will implicitly perform certain value conversions. As a result of this, weakly typed programming systems catch fewer errors at compile time, which can make debugging harder. According to this definition, C and C++ are weakly typed, as they automatically coerce many types e.g. ints and floats. E.g.

int a = 5;
float b = a;

They also allow ignore typedefs for the purposes of type comparison; for example the following is allowed, which would probably be disallowed in a strongly typed language:

typedef int Date;    /* Type to represent a date */
Date a = 12345;
int b = a;       /* What does the coder intend? */

C++ is stricter than C in its handling of enumerated types:

enum animal {CAT=0,DOG=2,ANT=3};
enum animal a = CAT;  /* NB The enum is optional in C++ */
enum animal b = 1;    /* This is a warning or error in C++ */
In other languages