0% found this document useful (0 votes)
20 views

Conversion

Uploaded by

imtiazroy000
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Conversion

Uploaded by

imtiazroy000
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Conversion

Conversion from one type to another type is known as implicit if it is to be


done automatically by the compiler. Implicit type conversions are also
called Coercion and coercion is limited in many languages.
Example: An integer may be converted to a real but real is not converted to an integer.
Conversion is said to be Explicit if the programmer writes something to do the
Conversion.
Tasks:
1. has to allow “Indexing is only on an array”
2. has to check the range of data types used
3. INTEGER (int) has a range of -32,768 to +32767
4. FLOAT has a range of 1.2E-38 to 3.4E+38.

Types of Type Checking:

There are two kinds of type checking:


1. Static Type Checking.
2. Dynamic Type Checking.

Static Type Checking:

Static type checking is defined as type checking performed at compile time. It


checks the type variables at compile-time, which means the type of the
variable is known at the compile time. It generally examines the program text
during the translation of the program. Using the type rules of a system, a
compiler can infer from the source text that a function (fun) will be applied to
an operand (a) of the right type each time the expression fun(a) is evaluated.
Examples of Static checks include:
• Type-checks: A compiler should report an error if an operator is
applied to an incompatible operand. For example, if an array variable
and function variable are added together.
• The flow of control checks: Statements that cause the flow of
control to leave a construct must have someplace to which to
transfer the flow of control. For example, a break statement in C
causes control to leave the smallest enclosing while, for, or switch
statement, an error occurs if such an enclosing statement does not
exist.
• Uniqueness checks: There are situations in which an object must
be defined only once. For example, in Pascal an identifier must be
declared uniquely, labels in a case statement must be distinct, and
else a statement in a scalar type may not be represented.
• Name-related checks: Sometimes the same name may appear two
or more times. For example in Ada, a loop may have a name that
appears at the beginning and end of the construct. The compiler
must check that the same name is used at both places.
The Benefits of Static Type Checking:
1. Runtime Error Protection.
2. It catches syntactic errors like spurious words or extra punctuation.
3. It catches wrong names like Math and Predefined Naming.
4. Detects incorrect argument types.
5. It catches the wrong number of arguments.
6. It catches wrong return types, like return “70”, from a function that’s
declared to return an int.

Dynamic Type Checking:

Dynamic Type Checking is defined as the type checking being done at run
time. In Dynamic Type Checking, types are associated with values, not
variables. Implementations of dynamically type-checked languages runtime
objects are generally associated with each other through a type tag, which is
a reference to a type containing its type information. Dynamic typing is more
flexible. A static type system always restricts what can be conveniently
expressed. Dynamic typing results in more compact programs since it is more
flexible and does not require types to be spelled out. Programming with a
static type system often requires more design and implementation effort.
Languages like Pascal and C have static type checking. Type checking is used
to check the correctness of the program before its execution. The main
purpose of type-checking is to check the correctness and data type
assignments and type-casting of the data types, whether it is syntactically
correct or not before their execution.
Static Type-Checking is also used to determine the amount of memory
needed to store the variable.
The design of the type-checker depends on:
1. Syntactic Structure of language constructs.
2. The Expressions of languages.
3. The rules for assigning types to constructs (semantic rules).

The Position of the Type checker in the Compiler:

Type checking in Compiler

The token streams from the lexical analyzer are passed to the PARSER. The
PARSER will generate a syntax tree. When a program (source code) is
converted into a syntax tree, the type-checker plays a Crucial Role. So, by
seeing the syntax tree, you can tell whether each data type is handling the
correct variable or not. The Type-Checker will check and if any modifications
are present, then it will modify. It produces a syntax tree, and after that,
INTERMEDIATE CODE Generation is done.
Type Systems

Dynamic Typing:
In Dynamic Typing, type checking is performed at runtime. For example,
Python is a dynamically typed language. It means that the type of a variable
is allowed to change over its lifetime. Other dynamically typed languages
are -Perl, Ruby, PHP, Javascript etc.
Let’s take a Python code example to see if a variable can change type:

## variable a is assigned to a string


a ="hello"
print(type(a))

## variable a is assigned to an integer


a = 5
print(type(a))

This confirms that the type of variable “a” is allowed to change and Python
correctly infers the type as it changes.
Let’s take another example of Dynamic Typing in Python :

## simple function
def add(a, b):
return a + b

## calling the function with string


print(add('hello', 'world'))

## calling the function with integer


print(add(2, 4))

In Python, we don’t really have a good idea of what are the types that this
function deals with and also what the type of the return value is going to
be.
Static Typing:
Static Typing is opposite to Dynamic Typing. In Static Typing, type checking
is performed during compile time. It means that the type of a variable is
known at compile time. For some languages, the programmer must specify
what type each variable is (e.g C, C++, Java), other languages offer some
form of type inference(e.g. Scala, Haskell).
With Static Typing, variables generally are not allowed to change types.
Let’s look at a simple example from a statically typed language. Consider
the following Java snippet;

String a;
a = "Java is good";

The first line declares that the variable “a” is bound to the string type at
compile time. In the second line, “a” is assigned a value which is not a string
object. For example, if we say, a =5, the compiler would raise an error
because of incompatible types.
Duck Typing
Duck Typing is a concept related to Dynamic Typing, where the type or the
class of an object is less important than the method it defines.Using Duck
Typing, we do not check types at all. Instead we check for the presence of a
given method or attribute.
The reason behind the name is the duck test: “If it looks like a duck, swims
like a duck, and quack like a duck, then it probably is a duck”.
Let’s take a simple Python code example to understand it clearly:

class Duck:
def quack(self):
print("Quack")

class Goose:
def quack(self):
print("Quack Quack")

class Dog:
def bark(self):
print("I just bark")
class ItQuacks:
def __init__(self, animal):
animal.quack()

ItQuacks(Duck())
ItQuacks(Goose())
ItQuacks(Dog())

The classes which have an implementation of “quack” method will be able


to call (Duck, Goose objects) but for the “Dog” class which does not provide
an implementation of “quack” method, gets a “AttributeError” if we try to
pass an object.

You might also like