you can write, but can you type?
DESCRIPTION
You can write, but can you type?. Gary Marsden Semester 2 – 2000. Why bother. Really, there is only one data type – ones and zeros. Anything else is an artificial creation However, artificial data types improve the correctness of programs Some programmers, however, live by - PowerPoint PPT PresentationTRANSCRIPT
Gary Marsden Slide 1University of Cape Town
You can write, but can you type?
Gary MarsdenSemester 2 – 2000
Gary Marsden Slide 2University of Cape Town
Why bother
Really, there is only one data type – ones and zeros. Anything else is an artificial creation
However, artificial data types improve the correctness of programs
Some programmers, however, live by– “Strong types are for weak minds”
Definition:A data type is a set of values, together with a set of operations on those values.
Gary Marsden Slide 3University of Cape Town
Declarations
Type is given to a value through the declaration of a variable
By declaring a variable, we are associating an identifier and a type to a value in a memory location
Technically, this is know as binding
– N.B. There are usually rules about acceptable identifier names (no punctuation or reserved words)
Gary Marsden Slide 4University of Cape Town
When is type allocated
There are many aspects to binding, the most important of which concerns when information is bound to a variable
There are two types– Static binding or Early binding when
information is determined at compilation– Dynamic binding or Late binding when
information is bound at run time
We will now look at the implications of these and the information which may be bound
Gary Marsden Slide 5University of Cape Town
Name & declaration binding
Variables are usually introduced in a program by a declaration
This will require us to look at– the connection (or bind) between type
information and a declaration and also– the connection (or bind) between the use of a
named variable and its declaration
Gary Marsden Slide 6University of Cape Town
Static typing – binding type information early
If type information is bound to a variable at compile time (static binding), this has a number of consequences– The compiler can check type consistency,
ensuring all executables are type correct• No type errors – fewer crashes• No run time checking – faster code
– The compiler can allocate storage (the size for each type is known) resulting in more efficient programs
Statically typed languages provide strong typing
Gary Marsden Slide 7University of Cape Town
Problems of static typing
Is usually very restrictive (Pascal)– loss of expressive power through premature
commitment– Also no generic procedures such as ‘sort’
Could flaunt type rules and force type conversions (coercion)– this is known as weak typing (like C) and leads
to errors
Is there an alternative?
Gary Marsden Slide 8University of Cape Town
Dynamic typing – binding type information late
We can increase flexibility in our typing, and still provide strong typing, by not checking type information until run time
This is called ensuring type consistencyThis makes prototyping solutions easier It also allows us to create generic
procedures (important in OO for polymorphism and overloading), but…– results in slower programs (run time checking)
Gary Marsden Slide 9University of Cape Town
Dynamic weak typing
Some dynamically typed languages don’t report errors if there is a type mis-match at run time
Instead they automatically coerce values so that they match
SmallTalk will coerce to strings, so if there are a lot of mismatches, every variable will eventually end up as a string
Gary Marsden Slide 10University of Cape Town
Static vs. Dynamic typing
Static– inconsistencies found at compile time– executed programs are type consistent– increases execution efficiency– easy to read programs
Dynamic– Much more flexible – better prototyping
Gary Marsden Slide 11University of Cape Town
Scope
We now look at the second sort of information binding – that between a declaration and use of a variable
This introduces the idea of scope; i.e. the area of a program where a particular declaration is valid
Gary Marsden Slide 12University of Cape Town
Static scope
float x,y;
void inn()
{
int y,z;
y = 34;
}
void main()
{
x = 3.141;
y = x;
}
‘main’ and ‘inn’ are both examples of blocks
variables declared within a block are local to the block – local variables
Variables declared in enclosing blocks are visible to inner blocks; variables in the inner block are not visible outside
Variable identifiers are bound to the most local declaration
Gary Marsden Slide 13University of Cape Town
Static scope in practice
The program works therefore as this ‘y’ in ‘main’ binds to the global declaration, not the inner one
This does not cause a type violation, as it refers to the closest (integer) declaration of ‘y’
float x,y;
void inn()
{
int y,z;
y = 34;
}
void main()
{
x = 3.141;
y = x;
}
Gary Marsden Slide 14University of Cape Town
Dynamic scope
With dynamic binding, lexical structure is ignored and the most recent declaration of a variable is bound to the name
The value printed is therefore ‘2.0’
With static binding, the result would have been ‘1’
Dynamic scoping makes analysing listings complex
int x;
void A()
{ printf(x);}
void B()
{ float x = 2.0;
A(); }
void main()
{ x = 1;
B(); A();
}
Gary Marsden Slide 15University of Cape Town
Allocating types
Typing is usually explicit requiring the programmer to allocate types to constants, variables, operators and functions– Improves readability– unless using FORTRAN which uses first letter of
variable name to denote type
Implicit checking can be performed by type inference systems
Gary Marsden Slide 16University of Cape Town
Summary
– Languages which guarantee type consistency for all types are usually referred to as strongly typed
• static => strong• strong > static (could be dynamic with type consistency
check)
– Languages are usually statically typed and scoped– Static typing usually has explicit declarations– Dynamic typing has explicit and implicit (more
usual)– Static, explicit typing is most common– Dynamic scoping is complex and very uncommon
Gary Marsden Slide 17University of Cape Town
Declaration – reference bindings
This refers to the lifetime or extent of a variable– i.e. how long storage space is bound to a variable
identifier
In most block structured languages this is only bound on block entry and collected in block exit– previous values of local variables are therefore lost
• hence ‘static’ variables (how should they be initialised?)
For dynamic structures, which are explicitly allocated, they must be explicitly released or have garbage collection
Gary Marsden Slide 18University of Cape Town
Reference – Value binding
In an assignment statement such as – x = x + 5;
The ‘x’ on the left refers to a location to put values and the ‘x’ on the right refers to an actual value
We need to de-reference the value on the right to get the value
Without assignment, names can be directly bound to values (not a reference) as in functional languages
Gary Marsden Slide 19University of Cape Town
Constants
Here, there is a direct binding of name to value (no reference) as the value cannot change
Some languages (e.g. ANSI C) require constants to be given a type
Other languages (K&R C) use macro pre-processors to lexically replace the constants before compilation
Gary Marsden Slide 20University of Cape Town
Basic types
Almost every language has basic predefined types which are used to construct other types
These come in two flavours– Simple: integer, character, real…– Enumerated: these are not predefined; are
used to create types of a particular subset of values• type colours = (red,green,blue); (Pascal)• type digit = (0..9); (Pascal)
– Enumerated declarations are usually restricted to ordinal types (which have a discrete range)
Gary Marsden Slide 21University of Cape Town
Complex types
These are really data structures. The language must provide type construction mechanisms to permit user defined data structures
The most common of these is the record which allows the creation of a heterogeneous data structure with named fields
Another flavour of this is the variant record, where different records of the same type may contain different fields– a concession to memory
Gary Marsden Slide 22University of Cape Town
Record Examples
TYPE Ibr = RECORD
I: integer;
B:boolean;
R:real;
END;
TYPE IoR = RECORD
CASE IsInt: BOOLEAN OF
TRUE: I: integer |
FALSE: R:real
END;
END;
RecordVariantRecord