Understanding Type Systems and Type Checking in Programming
Classified in Technology
Written at on English with a size of 3.95 KB.
Understanding Type Systems and Type Checking
A type can be defined as a set of terms within a language. These terms share several characteristics that allow them to interact or be subject to changes, simply by possessing these characteristics.
What is a Type System?
A type system consists of the basic theory used to associate each term of a declarative language with a type.
Key Components of Type Systems
- Type constructors: structures, unions, objects
- Pointers: type references
- Features like
a = sum();
Each semantic analyzer implements a type system.
- Static and Dynamic Testing
- Static: During compilation
- Dynamic: During execution
Type Checking Expressions
Verification of data types is done by assigning a type value to each lexical component.
These values are compared to verify that the data types match and are consistent. Otherwise, the calculations cannot be performed.
Type Conversion Explained
There are situations in which you have a value of a given type and want to store that value in a variable of a different type.
Some types may simply store the value without a cast; this is called automatic conversion.
This is only possible in a programming language if the compiler recognizes that the destination variable has enough precision to contain the source value.
- In Java, you can store a
byte
value in anint
variable sinceint
is more precise thanbyte
. - This is called widening or promotion, as the smaller type widens or promotes to the larger type. However, if you want to assign an
int
value to abyte
variable, you need to perform an explicit type conversion. - In some cases, you can do the conversion, but you might lose data, such as when passing a
float
value to an integer.
This is called narrowing since you explicitly narrow the value to fit the destination.
Type conversion is done by putting the type name in parentheses before the value, e.g., (type) value
.
Types of Verification: Static and Dynamic
There are two types of verification: static and dynamic.
Verification helps to prevent most bugs. Examples:
- Type testing: To see if the operator applied to operands is correct.
- Flow control check: You should verify that the instructions that change the flow of a program are valid. Example:
break
,goto
. - Uniqueness proof: Define an object only once.
- Related names testing: The same name must appear twice. Variables that are declared but not used.
Type checking is more complicated. Other checks are routine.
- The
%
operator requires that both operands are integers. +
is a functionsum(a, b)
that is overloaded for different data types.
Evaluation rules are designed so that numerical values are converted to a higher level or type, and a data pointer only points to the declared data type.
- Some languages review the size of arrays statically (like Java), while others do it dynamically (at runtime).
- Differentiate the use of
+
,*
with integers and pointers (pointer arithmetic).
The set of rules defined for the verification of data types is called the type system.
Most often, error recovery is omitted because the program does not end, but it might not get the desired values.
Usually, in the parsing stage, types are added to the symbol table.
- A review of the syntax tree is performed to check the assigned types.
- Actions are added in a descending parser (top-down).