Type Checking and Type Conversion

Type checking means checking that each operation should receive proper number of arguments and of proper data type.

Like

A=B*j+d;

* and - are basically int and float data types based operations and if any variable in this A=B*j+d;Is of other than int and float then compiler will generate type error.

Two ways of Type Checking:

1) Dynamic Type Checking:

• It is done at runtime.

• It uses concept of type tag which is stored in each data objects that indicates the data type of the object.

Example:

An integer data object contains its'type' and 'values' attribute.

so Operation only be performed after type checking sequence in which type tag of each argument is checked. If the types are not correct then error will be generated.

• Perl and Prolog follow basically dynamically type checking because data type of variables A+B in this case may be changed during program execution.

• so that type checking must be done at runtime.

Advantages of Dynamic Type:

• It is much flexible in designing programs or we ca say that the flexibility in program design.

• In this no declarations are required.

• In this type may be changed during execution.

• In this programmerare free from most concern about data type.

Disadvantage of Dynamic Type:

1) difficult to debug: We need to check program execution paths for testing and in dynamic type checking, program execution path for an operation is never checked.

2) extra storage: Dynamic type checking need extra storage to keep type information during execution.

3) Seldom hardware support : As hardware seldom support the dynamic type checking so we have to implement in software which reduces execution speed.

Type checking: Static Type Checking:

Static Type Checking is done at complete time.

Information needed at compile time is provided- by declaration- by language structures.

The information required includes:

1) for each operation: The number, order, and data type, of its arguments.

2) For each variables: Name and data type of data object.

Example-

A+B

in this type of A and B variables must not be changed.

3) for each constant: Name and data type and value

const int x=28;

const float x=2.087;

In this data type, the value and name is specified and in further if checked value assigned should match its data type.

Advantages of Static Type Checking:

1) compiler saves information:- if that type of data is according to the operation then compiler saves that information for checking later operations which further no need of compilation.

2) checked execution paths: As static type checking includes all operations that appear in any program statement, all possible execution paths are checked, and further testing for type error is not needed. So no type tag on data objects at run-time are not required, and no dynamic checking is needed.

Disadvantages of Static Type Checking

: It affects many aspects of languages

1) declarations

2) data control structures

3) provision of compiling separately some subprograms.

Strong Typing:

If we change detect all types of errors statically in a program, we can say that language is' strongly typed'.

It provides a level of security to our program.

Example

f:s-> R

In this function f mail signature s generate output R and R is not outside the range of R data type.

IF every operation is type safe then automatically language is strongly typed.

Example of strongly typed languages are:

C,Java, C++, RubyRail, smalltalk, python.

Type infer:- In this, like in ML, the language implementation will infer any missing type information from other declared type.

Example:

funarea(length:int, width:int):int= length *width;

This is the standard declaration which tells length and width of int data type and its return type is int and function name area. But leaving any two of these declarations still leaves the function will only one interpretation. Knowing that * can multiply together either two reals or two integers. ML interprets the following as equivalent to the previous example.

Funarea(length,width)int= length*weight;

Funarea(length:int,width)= length*weight;

Funarea(length,width:int)= length*weight;

However:

Funarea(length,width)= length*weight;

Is invalid as it is now ambiguous as to that type of arguments. They could all be int or they could be real.




Facebook Likes

Youtube