DEV Community

himank
himank

Posted on

Demystify dynamically or statically typed language?

It is almost impossible to avoid terms like statically typed and dynamically typed etc when you start learning a new programming language. Although, Everybody talks about the kind of typing any language has but, rarely it is given importance by the new learners. In this post, I would like to give a brief about types, type system, static and dynamic typing.

What are types in programming?

A type defines a set of possible values and a set of actions or operations that can be performed on an object. Every object has a type.

Broadly, there are two types of language implementations. Some languages are statically typed and others dynamically typed. A very famous example of a statically typed language is C, C++, Java, examples of a dynamically typed language is Python, Ruby, Javascript etc.

Let us see what static type checking means?

Static type checking checks the input program against the type rules defined in the language after the program parses successfully (that is there are no syntax errors in it). It is done without giving any input to the program being checked.
The aim of static type checking is to reject programs early, that may cause an error if they will execute. If the program does not parse, It will also result in an error. This error is different from what we get from the type checker. This error is called parser error or Syntax error. The static type checker would typically give a type error — something like, operation on incompatible types etc.

Whatever the static checker performs is part of the language definition and different languages may define different rules.

Static type checking is achieved typically through a type system. The main purpose of a type system is to reduce possibilities for bugs in programs by defining interfaces between different parts of a program, and then checking that the parts have been connected in a consistent way.

Dynamic type checking :

In a dynamic language, type checking occurs at run-time. Many languages like python, ruby etc check the type safety of a program at runtime.
Typically, It is done by tagging each run-time object with a tag (that is a reference to a type) which has all the type information. This information is used to check for type errors.

Many languages implement some form of dynamic type checking even if they have static type checking because many useful features and non-trivial properties are difficult to verify statically. A good example of this is downcasting. A dynamic check is needed to verify that the operation is safe or not during downcasting.

By definition, dynamic type checking may cause a program to fail at runtime. In some programming languages, it is possible to anticipate and recover from these failures. In others, type-checking errors are considered fatal.

There are other errors that a type system typically does not prevent (such as logical errors). For example, if a program uses addition operator instead of multiplication, this is still a valid program but not the intended one.

Top comments (0)