typing


 Development Techniques Seminar
                         s03e04
... definitions ...
An informal definition:
A type is metadata describing a chunk of memory that classifies
the data store...
... the typing problem ...
In a OOL, the only event than can happen during the
execution of an object-oriented system:

ob...
... typing rules ...

Every entity must be declared as being of a certain type.

In every assignment and in every routine ...
... implitic type conversions ...
n: INTEGER

n := 0.0
n := 1.0
n := -3,67
n := 3.67 - 3.67

With strict typing, assigning...
... static and dynamic typing ...
                    (... another holy war ...)

Although intermediate variants are possi...
... static ...
Languages: C, C++, Java, Pascal, Ada,...

Arguments:
   catching errors earlier (at compile type or interpr...
... dynamic (i) ...

Languages: Smalltalk, Python, PHP, Ruby, Perl, ...

Martin Fowler:
  Static typing is a premature too...
... dynamic (ii) ...

Robert Martin:

"Too many systems crashed in the field due to silly typing
errors."

"I was dependin...
... dynamic (iii) ...
Brucke Eckel (not agains static typing):

"I think that statically typed languages give the illusion...
... dynamic (iV) ...
More quotes:

"The early warnings/errors are not the real disadvantage. The
point is more that static...
... binding (i)...

Don´t confuse with typing.

If we have x.f(arg)...

Typing question: When do we now for sure that at r...
... binding (ii)...


                   static typing    dynamic typing

static binding     ADA, Pascal, C++ Assembly,
  ...
... strong and weak typing (i) ...
    Don´t confuse static / dynamic with strong / weak.

Weak typing: Allows incorrect m...
... strong and weak typing (ii)...
WeakTyping / StrongTyping / StronglyTypedWithoutLoopholes.

There are well known errors...
... more definitions (i) ...
Typing dimensions:
   is there a nontrivial type associated with each declaration?
        St...
... more definitions (ii) ...
StaticTyping: Type checking before runtime.
DynamicTyping: Type checking at runtime.
Strongl...
... more definitions (iii) ...
   TypeInference: Analysis of a program to infer the types of some or all
   expressions (H...
... covariance (i) ...
Covariance: change of argument types in redefinitions.

In general, covariance allows you to expres...
... covariance (ii)...
Covariance and descendant hiding can introduce errors that the
language has to resolve.
Problems ar...
... covariance in c++...
class A {
  virtual ~A();
  virtual A * f();
  ...
};

class CovB : public A {
  virtual CovB * f...
... PHP (i) ...

Our most used language is:
   dynamic typed
   with type hinting
$a = 1; $a = "hello";

function foo(arra...
... PHP (i) ...

Our most used language is:
   dynamic typed
   with type hinting
                     Catchable fatal err...
... PHP (ii)...
class A {
   public function foo1($arg = "hola") { echo "$arg n"; }
   public function foo2($arg = NULL) {...
... PHP (ii)...
class A {
   public function foo1($arg = "hola") { echo "$arg n"; }
   public function foo2($arg = NULL) {...
... PHP (iii)...
class A {
   // public function foo1() { echo "A::foo1 n";}
   private function foo2() { echo "A::foo2 n"...
... PHP (iii)...
class A {
   // public function foo1() { echo "A::foo1 n";}
   private function foo2() { echo "A::foo2 n"...
... references and links ...

OOSC2nd
Typing: Strong vs. Weak, Static vs. Dynamic
A "fair and balanced" look at the static...
... fin ...




                    César Ortiz
              cesar@tuenti.com
Upcoming SlideShare
Loading in...5
×

DTS s03e04 Typing

1,054

Published on

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.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,054
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
8
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

DTS s03e04 Typing

  1. 1. typing Development Techniques Seminar s03e04
  2. 2. ... 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.
  3. 3. ... 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 obj arg no acceptable argument to f Type safety: not possible to apply invalid operations
  4. 4. ... 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).
  5. 5. ... implitic type conversions ... n: INTEGER n := 0.0 n := 1.0 n := -3,67 n := 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, ...).
  6. 6. ... 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.
  7. 7. ... static ... Languages: C, C++, Java, Pascal, Ada,... Arguments: catching errors earlier (at compile type or interpreting time) => less cost in large projects readability efficiency and less memory consumption Better 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)
  8. 8. ... 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 bugs more flow programming (it is difficult to put the experience in words)
  9. 9. ... 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."
  10. 10. ... 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"
  11. 11. ... 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."
  12. 12. ... 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.
  13. 13. ... binding (ii)... static typing dynamic typing static binding ADA, Pascal, C++ Assembly, scripting dynamic binding C++, Eiffel Smalltalk
  14. 14. ... 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 perl strong: python and ruby
  15. 15. ... 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)
  16. 16. ... 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
  17. 17. ... 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 typesafety StronglyTypedWithoutLoopholes: 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.
  18. 18. ... 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 typing Dynamic Static Type inference Soft typing
  19. 19. ... 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.
  20. 20. ... 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 assignment s.share(g) -- the type of the argument of share is different in -- each class (redeclaration) p: POLYGON; r:RECTANGLE ... !!r; -- creation p := r -- polymorphic assignment p.add_vertex(...) -- ¿accepted in the rectangle? (defined -- in the father and hidden in the child)
  21. 21. ... 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.
  22. 22. ... PHP (i) ... Our most used language is: dynamic typed with type hinting $a = 1; $a = "hello"; function foo(array $arg) { echo "$arg n"; } foo($a);
  23. 23. ... PHP (i) ... Our most used language is: dynamic typed with type hinting Catchable fatal error: Argument 1 passed to foo2() must be an array, string $a = 1; $a = "hello";given... function foo(array $arg) { <---- !!!!!!!!! hello echo "$arg n"; } foo($a);
  24. 24. ... 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);
  25. 25. ... PHP (ii)... class A { public function foo1($arg = "hola") { echo "$arg n"; } public function foo2($arg = NULL) { if ($arg === NULL) $arg = "hola"; hello echo "$arg n"; hola } A::foo3 public function foo3($arg1) { echo "A::foo3 n";} public function foo4(array $arg1) { echo "A::foo4 n";} Warning: Missing argument 2 for B::foo4(), ... } class B extends A { A::foo4 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);
  26. 26. ... 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();
  27. 27. ... PHP (iii)... class A { // public function foo1() { echo "A::foo1 n";} private function foo2() { echo "A::foo2 n";} } B::foo2 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();
  28. 28. ... references and links ... OOSC2nd Typing: Strong vs. Weak, Static vs. Dynamic A "fair and balanced" look at the static vs. dynamic typing schism Brucke Eckel - 11-16-04 Static vs. Dynamic Weighing into the static vs dynamic typing debate Category Language typing Covariance in C++ Covariance (wikipedia) Type system (wikipedia)
  29. 29. ... fin ... César Ortiz cesar@tuenti.com
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×