Upload
tuenti
View
1.054
Download
0
Tags:
Embed Size (px)
DESCRIPTION
The presentation starts with some basic theory on types. Later, different classifications for type systems are described, with the static/dynamic and strong/weak dimensions the ones we spend more time on.The third topic addressed is how the mix of polimorsfism with covariance and descendant hiding affects the type system, and what new problems arise and have to be addressed by the type system.We finished the presentation with a discussion about pros and cons of the PHP type system.
Citation preview
typing
Development Techniques Seminar s03e04
... definitions ...
An informal definition:A type is metadata describing a chunk of memory that classifies the data stored there. This classification usually implicitly specifies what sort of operations may be performed on the data.Common types include primitive types (as strings or numbers), container types (as arrays), pointers or references and complex types (as classes or interfaces).
In a pure OOL, every instance (of data) is an object.
... the typing problem ...
In a OOL, the only event than can happen during the execution of an object-oriented system:
obj.f(arg)
Type violation/failure:no f applicable to objarg no acceptable argument to f
Type safety: not possible to apply invalid operations
... typing rules ...
Every entity must be declared as being of a certain type.
In every assignment and in every routine the type of the source must conform the type of the target (type conformance rule).
x := y
In a call of the form x.f(arg), f must be a feature of x(feature call rule).
... implitic type conversions ...
n: INTEGER
n := 0.0n := 1.0n := -3,67n := 3.67 - 3.67
With strict typing, assigning a float value should fail.Allowing that is ambiguous (do we want the rounded or the truncated version?).
For implicit conversions to work in a strict typed environment additional functions are needed (truncate, round, ...).
... static and dynamic typing ...(... another holy war ...)
Although intermediate variants are possible two main approaches:
Dynamic typing: type verification at execution time.Static typing: type verification performed on the text, before execution.
A dynamically typed language has no type declarations. 'Entities' simply become associated with whatever values the execution attaches to them.
... static ...
Languages: C, C++, Java, Pascal, Ada,...
Arguments:catching errors earlier (at compile type or interpreting time) => less cost in large projectsreadability efficiency and less memory consumptionBetter IDE support
(In a dynamically typed program, it's easy for a human to tell what type something is likely to be, but no way for a machine to say for sure what type something is)
... dynamic (i) ...
Languages: Smalltalk, Python, PHP, Ruby, Perl, ...
Martin Fowler:Static typing is a premature tool to catching bugs, but if you use TestDrivenDevelopment (as you should) you are already catching them additionally to more types of bugsmore flow programming (it is difficult to put the experience in words)
... dynamic (ii) ...
Robert Martin:
"Too many systems crashed in the field due to silly typing errors." "I was depending less and less on the type system for safety. My unit tests were preventing me from making type errors. "
"The flexibility of dynamically typed langauges makes writing code significantly easier. Modules are easier to write, and easier to change. There are no build time issues at all. Life in a dynamically typed world is fundamentally simpler."
... dynamic (iii) ...Brucke Eckel (not agains static typing):
"I think that statically typed languages give the illusion of program correctness. ""llusion that static type checking can solve all of your problems, followed by the conclusion that more static type checking is always better.""Additional forms of static type checking are often added to a language without regard to the actual cost. ""In extreme cases you spend all your time arguing with the compiler.""In general, my attitude is that static typing is desirable as long as it doesn't cost you too much""With a dynamic language the model becomes the code"
... dynamic (iV) ...More quotes:
"The early warnings/errors are not the real disadvantage. The point is more that static typechecks with existing languages force you to structure programs to suit the particular, single static type system that the language in question uses."
"Combined with testing, assertions, contracts, and other good practices (including a more disciplined use of types than the one I've just described), you can scale up from a prototype to a more disciplined system without changing to a statically-checked language."
... binding (i)...
Don´t confuse with typing.
If we have x.f(arg)...
Typing question: When do we now for sure that at run time there will be an operation corresponding to f and applicable to the object attached to x (with the argument arg). > Addresses the existance.
Binding question: Which operation will be executed by the call? > Addresses the choice of the right one.
... binding (ii)...
static typing dynamic typing
static binding ADA, Pascal, C++ Assembly, scripting
dynamic binding C++, Eiffel Smalltalk
... strong and weak typing (i) ...
Don´t confuse static / dynamic with strong / weak.
Weak typing: Allows incorrect messages to be sent to objects
Just an example of what you could do in a weak typing language: "1" + 1
Languages with unchecked casts (as C/C++) 'could' also be considered weak.
Examples of dynamic languages: weak: php and perlstrong: python and ruby
... strong and weak typing (ii)...
WeakTyping / StrongTyping / StronglyTypedWithoutLoopholes.
There are well known errors with numeric castings.C has the problem of the unconditional type cast.
C++ introduced new casts but maintained the old one and the unsafe new static cast (dynamic cast is safe)
Java, provides static type checking and its runtime cast checking sharply limits the consequences of a bad cast.
Dynamic languages work like java (withouth static typing)
... more definitions (i) ...Typing dimensions:
is there a nontrivial type associated with each declaration?StaticTyping: yes DynamicTyping: no SoftTyping: optional
if there is, are these types declared explicitly in the source code? ManifestTyping: yes TypeInference: optional
does the possibility of a type failure cause a compile-time error? StaticTyping: yes SoftTyping or DynamicTyping: no
is the type system strictly enforced, with no loopholes or unsafe casts?
StronglyTypedWithoutLoopholes: yes WeakTyping: no
... more definitions (ii) ...StaticTyping: Type checking before runtime.DynamicTyping: Type checking at runtime.StronglyTyped: All operations are checked (either statically or at run-time) for type correctness, with the exception of only a small number of loopholes/escapes from typesafetyStronglyTypedWithoutLoopholes: StronlyTypes but withough Loopholes.WeaklyTyped: Type failures are possible and cause UndefinedBehavior.ManifestTyping: Requires a type annotation to be given explicitly for each declaration.SoftTyping: Where the type checker can prove that the program is type safe, everything is cool. Where the type checker can't prove correctness it informs the programmer and inserts appropriate type checks, but doesn't reject the program.
... more definitions (iii) ...TypeInference: Analysis of a program to infer the types of some or all expressions (Haskell).ImplicitTyping: Typing system which requires few or no type annotations.DuckTyping, StructuralTyping, MockTyping (tecnique),...
Examples of flavours:Static typing:
Manifest typing (C, C++)Type Inference (Haskell)
Implicit typingDynamicStatic Type inferenceSoft typing
... covariance (i) ...Covariance: change of argument types in redefinitions.
In general, covariance allows you to express more information in the derived class interface than is true in the base class interface. The behaviour of a derived class is more specific than that of a base class, and covariance expresses (one aspect of) the difference.
In many strictly-typed languages (with the notable exception of Eiffel), subclassing must allow for substitution. That is, a child class can always stand in for a parent class. This places restrictions on the sorts of relationships that subclassing can represent. In particular, it means that arguments to member functions can only be contravariant and return types can only be covariant.
... covariance (ii)...
Covariance and descendant hiding can introduce errors that the language has to resolve.Problems arise with polymorphism.
s: SKIER; b:BOY; g:GIRL...!!b; !!g;s:=b; -- Polymorphic assignments.share(g) -- the type of the argument of share is different in -- each class (redeclaration)
p: POLYGON; r:RECTANGLE...!!r; -- creationp := r -- polymorphic assignmentp.add_vertex(...) -- ¿accepted in the rectangle? (defined -- in the father and hidden in the child)
... covariance in c++...class A { virtual ~A(); virtual A * f(); ...};
class CovB : public A { virtual CovB * f(); ...};
class NonCovB : public A { virtual A * f(); ...};
Also the templates are a covariance mechanism.
... PHP (i) ...
Our most used language is:dynamic typedwith type hinting
$a = 1; $a = "hello";
function foo(array $arg) { echo "$arg \n";}
foo($a);
... PHP (i) ...
Our most used language is:dynamic typedwith type hinting
$a = 1; $a = "hello";
function foo(array $arg) { echo "$arg \n";}
foo($a);
Catchable fatal error: Argument 1 passed to foo2() must be an array, string given...
hello <---- !!!!!!!!!
... PHP (ii)...class A { public function foo1($arg = "hola") { echo "$arg \n"; } public function foo2($arg = NULL) { if ($arg === NULL) $arg = "hola"; echo "$arg \n"; } public function foo3($arg1) { echo "A::foo3 \n";} public function foo4(array $arg1) { echo "A::foo4 \n";}}class B extends A { public function foo1($arg = "hello") { parent::foo1($arg); } public function foo2($arg = NULL) { parent::foo2($arg); } public function foo3($arg1, $arg2) { parent::foo3($arg1); } public function foo4($arg1, $arg2) { echo "B::foo4 \n"; }}
$b = new B();$b->foo1(); // what is the output?$b->foo2(); // what is the output?$b->foo3(1,2); // changing signature allowed$b->foo4(1);
... PHP (ii)...class A { public function foo1($arg = "hola") { echo "$arg \n"; } public function foo2($arg = NULL) { if ($arg === NULL) $arg = "hola"; echo "$arg \n"; } public function foo3($arg1) { echo "A::foo3 \n";} public function foo4(array $arg1) { echo "A::foo4 \n";}}class B extends A { public function foo1($arg = "hello") { parent::foo1($arg); } public function foo2($arg = NULL) { parent::foo2($arg); } public function foo3($arg1, $arg2) { parent::foo3($arg1); } public function foo4($arg1, $arg2) { echo "A::foo4 \n"; }}
$b = new B();$b->foo1(); // what is the output?$b->foo2(); // what is the output?$b->foo3(1,2); // changing signature allowed$b->foo4(1);
hello hola A::foo3
Warning: Missing argument 2 for B::foo4(), ...
A::foo4
... PHP (iii)...class A { // public function foo1() { echo "A::foo1 \n";} private function foo2() { echo "A::foo2 \n";}}
class B extends A { // Fatal error: Access level to B::foo1() // must be public (as in class A) // private function foo1() { echo "B::foo2 \n"; } public function foo2() { echo "B::foo2 \n"; }}
$b = new B();// $b->foo1();$b->foo2();
... PHP (iii)...class A { // public function foo1() { echo "A::foo1 \n";} private function foo2() { echo "A::foo2 \n";}}
class B extends A { // Fatal error: Access level to B::foo1() // must be public (as in class A) // private function foo1() { echo "B::foo2 \n"; } public function foo2() { echo "B::foo2 \n"; }}
$b = new B();// $b->foo1();$b->foo2();
B::foo2
... references and links ...
OOSC2ndTyping: Strong vs. Weak, Static vs. DynamicA "fair and balanced" look at the static vs. dynamic typing schismBrucke Eckel - 11-16-04 Static vs. DynamicWeighing into the static vs dynamic typing debate Category Language typingCovariance in C++Covariance (wikipedia)Type system (wikipedia)
... fin ...
César [email protected]