Agile FAQs
  About   Slides   Home  

 
Managed Chaos
Naresh Jain's Random Thoughts on Software Development and Adventure Sports
     
`
 
RSS Feed

Recent Thoughts
Tags
Recent Comments

Dynamic Typing is NOT Weak Typing

Till very recently I did not know the clear distinguish between Static/Dynamic and Strong/Weak typing. Thanks to Venkat for enlightening me.

Dynamic typing: Variables’ type declarations are not mandatory and they will be generated/inferred on the fly, by their first use.

Static typing: Variable declarations are mandatory before usage, else results in a compile-time error.

Strong typing: Once a variable is declared as a specific data type, it will be bound to that particular data type. You can explicitly cast the data type though.

Weak typing: Variables are not of a specific data type. However it doesn’t mean that variables are not “bound” to a specific data type. In weakly typed languages, once a block of memory is associated with an object it can be reinterpreted as a different type of object.

One thing I’ve realized, Strong vs. Weak and Dynamic vs. Static is a continuum rather than an absolute measure. For instance, SmallTalk is more strongly typed compared to Python which is more strongly typed than JavaScript.

There seem to be two major lines along which strong/weak typing is defined:

  • The more type coercions (implicit conversions) for built-in operators the language offers, the weaker the typing. (This could also be viewed as more built-in overloading of built-in operators.)
  • The easier it is in a language, or the more ways a language offers, to reinterpret a memory block (associated with a data value) as a different type, the weaker the typing.

In strongly typed languages if you cast to the wrong type, you get a runtime cast exception. While in weakly typed languages, your program might crash if you are lucky. Usually it leads to wrong behavior.

In most static languages you need to specify the data type at declaration. However in languages like Scala, you don’t need to specify the data types, the compiler is smart enough to infer the data types based in the context in which its used.

Also if you don’t have a compiler, then the language is surely dynamic language. However the inverse is not true. For example, Groovy is compiled, yet its a dynamic language.

In Strongly typed Dynamic languages, the type inference is postponed till runtime. This has many advantages:

  • One can achieve greater degree of polymorphism
  • One does not need to keep fighting the compiler by doing trivial type casting
  • One gets greater flexibility by deferring the implementation to a later point. i.e. the actual type verification is postponed to runtime; allowing us to modify the structure of the program between compile time and runtime.

I always thought weakly typed, dynamic language would be a disaster. However both VB and PHP (amongst most popular languages in the last 2 decades) fall into this category.

Having said that, these days I see more and more languages are strongly typed. Also the ability to infer types is gaining a lot of traction.

What do you prefer in your programming language and why?

  • Daniel Sobral

    It is not that static type requires variable declaration — though that is true in the most popular statically typed languages (Java, C, C++).

    What makes static typing is that the compiler makes a proof over certain characteristics of your program — most commonly, about the data structure representation (size, fields), and methods in an OO language. So, in C, the type “int” provides guarantees that a certain amount of memory will be allocated, that it will be placed in such a manner as to not cause memory alignment exceptions, and that some operators can act on it. On Java, you get a guarantee that all methods being called are suported by the objects they are being called upon (well, to  certain extent — null, for instance, breaks that guarantee).

    Types, however, are not restricted to structure: they can also relate to behavior. An example of such in Java are the exceptions — they indicate not something about structure, but about behavior. Lots of research nowadays relate to using types to make proofs about concurrency (such as absence of deadlocks), side effect tracking, resource consumption (safe sharing of mutable data), etc.

    But, at any rate, none of this is related to declaration. Consider, for instance, types used for resource tracking — let’s say, a mutable object. The compiler knows which methods in the object mutate it, and which do not. So, say a class C has a mutating method M, and an object O has a method F receiving an object of type C as parameter, and then calling M on it. It is possible to say that method F consumes the object of type C passed to it, and detect any usage of said object after calling F, tagging it as an error. No declaration is required, and the compiler will prove that any correct program (one that has compiled successfully) does not consume resources that have been spent.

    A statically typed program is one over which certain correctness characteristics have been proven, though that doesn’t mean that the program is correct, or that a program with “dynamic typing” is incorrect.

  • Sucker Milano

    Bankruptcy  Records

    https://www.bankruptcylive.com/bankruptcy-records/

    Searching for Bankruptcy
    Discharge records? Get official bankruptcy discharge papers records $9.99,
    complete bankruptcy file for $29.99. online. Fast Delivery by email same day
    within 1 – 6 hours.


    Licensed under
Creative Commons License