What is Static Type Checking?

Compiler DesignProgramming LanguagesComputer Programming

Type checking is the activity of providing that the operands of an operator are of compatible types. A compatible type is one that is legal for the operator or is enabled under language rules to be implicitly modified by compiler-generated code to a legal type. This automatic conversion is known as coercion. A type error is the application of an operator to an operand of an improper type. It can illustrate the concept of type checking considers the following statement.

c:=a + 3 * b;

Here b should be of a type that allows multiplication by an integer. Similarly, the operands for addition and assignment can be evaluated.

If all binding of variables to type is static in a language, then type checking can virtually continually be completed statistically. It is more to identify errors at compile time than at runtime because the previous correction is generally less expensive. Therefore, static type checking involves examining the program text, usually during translation.

Using the rules of a type system, a compiler can infer from the source text that a function f will be applied to an operand a of the right type, each time the expression f (a) is evaluated. The required data for static type checking is generally supported in part by declarations that the programmer supports and in part by different language structures.

Advantage of Static Type Checking

There are the following advantages of static type checking which are as follows −

  • The early detection of errors at compile time.

  • It is used to checking all execution paths.

  • There is no requirement of type tags on data objects at runtime.

  • A consequential gain in the adaptability of storage use.

  • A consequential gain in execution rate.

Disadvantages of Static Type Checking

There are the following disadvantages of static type checking which are as follows −

  • It can reduce programmer flexibility.

  • Many languages like APL and SNOBOL4, because of their dynamic type binding, enable only dynamic type checking.

  • In languages without declarations, there is no static type checking is possible.

  • Static type checking likely to influence declarations, data control structures, and supplies for independent compilation of subprograms.

  • Static type checking is complex when a language enables a memory cell to save values of multiple types at various times during execution.

Published on 22-Oct-2021 11:55:14