Question

Is the quality of a language where it's not required to declare a variables type (such as PHP and Perl) known as weak typing or dynamic typing? I'm having trouble getting my head around the two terms.

Am I right that dynamic/static typing pertains to type conversion whereas weak/strong typing pertains to the deceleration of a variable?

Was it helpful?

Solution

According to: http://en.wikipedia.org/wiki/Type_system#Static_and_dynamic_type_checking_in_practice

Weak typing means that a language implicitly converts (or casts) types when used.

whereas:

A programming language is said to use static typing when type checking is performed during compile-time as opposed to run-time.

So, strong/weak and static/dynamic are two different dimensions. A language will be one of strong/weak, and also one of static dynamic. For instance, Ruby and Javascript are both dynamically typed, but Ruby is strongly typed while Javascript is weakly typed. That is, in Ruby the following code with give an error:

1.9.2p290 :001 > 'a'+1
TypeError: can't convert Fixnum into String

whereas in JavaScript, you get:

> 'a'+1
>> 'a1'

So, a strongly typed language requires you to convert two variables to the same type in order to combine them (eg. using 1.to_s), while a weakly typed language will attempt to coerce the two variables into the same type using some extra built-in language logic - in JavaScript's case, combining anything with a String will convert it into a String value.

See: http://www.artima.com/weblogs/viewpost.jsp?thread=7590 for a further explanation.

OTHER TIPS

In simple terms, strong typing has more to do with how objects are bound (essentially early binding vs. late binding) than they have to do with how they are declared.

Let's say in C# I have this class:

public class Foo
{
    public int Bar;
    public double Baz;
}

And I declare a variable of type foo:

var myFoo = new Foo();

When I reference Foo, like this:

foo.

Visual Studio will display a list containing Bar and Baz when I type the ., because it already knows that myFoo contains those members; it is of type Foo. This is strong typing; it means that if I misspell Bar or Baz, my program won't even compile.

However, let's say I declare a variable of type dynamic This causes object binding to be deferred until the program is executed:

dynamic myFoo = new Foo();
myFoo.Grapes = 6;

This will compile. I won't get an error until the program is run; a runtime exception will be thrown because Grapes doesn't exist on Foo.

This is an old question, but for future readers this great article may clear things up: http://blogs.perl.org/users/ovid/2010/08/what-to-know-before-debating-type-systems.html

It's kind of long but it definitively worth it.

Strong and Weak Typing:

Probably the most common way type systems are classified is "strong" or "weak." This is unfortunate, since these words have nearly no meaning at all. It is, to a limited extent, possible to compare two languages with very similar type systems, and designate one as having the stronger of those two systems. Beyond that, the words mean nothing at all.

Static and Dynamic Types

This is very nearly the only common classification of type systems that has real meaning. As a matter of fact, it's significance is frequently under-estimated [...] Dynamic and static type systems are two completely different things, whose goals happen to partially overlap.

A static type system is a mechanism by which a compiler examines source code and assigns labels (called "types") to pieces of the syntax, and then uses them to infer something about the program's behavior. A dynamic type system is a mechanism by which a compiler generates code to keep track of the sort of data (coincidentally, also called its "type") used by the program. The use of the same word "type" in each of these two systems is, of course, not really entirely coincidental; yet it is best understood as having a sort of weak historical significance. Great confusion results from trying to find a world view in which "type" really means the same thing in both systems. It doesn't.

Explicit/Implicit Types:

When these terms are used, they refer to the extent to which a compiler will reason about the static types of parts of a program. All programming languages have some form of reasoning about types. Some have more than others. ML and Haskell have implicit types, in that no (or very few, depending on the language and extensions in use) type declarations are needed. Java and Ada have very explicit types, and one is constantly declaring the types of things. All of the above have (relatively, compared to C and C++, for example) strong static type systems.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top