Search code examples
language-agnosticstrong-typingstatic-typingdynamic-typingweak-typing

Is the quality of a language where it's not required to declare a variables type an example of weak typing or dynamic typing


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?


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.