1 / 14

Types

Object Oriented Programming 236703 Spring 2007. Types. Values. 5 Color.RED "abc" new String[20] new Scanner(" c:/temp/in.txt ") ‏ new Properties() ‏. Supported Operations. int x = 5; Color.RED == null "abc" .equalsIgnoreCase ("ABC") ‏ (new String[20]) [0]

herman
Download Presentation

Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Oriented Programming 236703 Spring 2007 Types

  2. Values 5 Color.RED "abc" new String[20] new Scanner("c:/temp/in.txt")‏ new Properties()‏

  3. Supported Operations int x = 5; Color.RED == null "abc".equalsIgnoreCase("ABC")‏ (new String[20])[0] new Scanner("c:/temp/in.txt").next()‏ new Properites(0).getProperty("a")‏

  4. Unsupported Operations int x = 5.9; Color.RED.equals(5,5)‏ "abc".remove(0)‏ (new String[20])[0][0] = "xyz"; new Scanner("c:/temp/in.txt") = null Integer.parseInt(Properites(0))‏

  5. Dynamic Typing • The dynamic typing approach: • Compile-time: Allow all operations • Run-time: Before carrying out an operation, check that it is supported by the value • Requirement: • A run-time data structure that determines the “support” question • Consequences: • Assignments to variables never fail • No need to define types (of variables, return types, etc.)‏ • Flexibility • Run-time errors if we are not careful

  6. Static Typing • The static typing approach: • Compile-time: Check that operations are correct • Run-time: Just carry out the operation • The idea: The compiler “simulates” the evaluation of expressions • Compile-time computation of the type of the expression • Corresponds to the run-time computation of the result of the expression • If a type cannot be computed => Compilation error • The computed type of subexpressions is used in computing the type of bigger expressions

  7. Static Typing (cont.)‏ • Requirements • Variables have types • Methods have return types • Assignments are checked by the compiler • Message sends are checked by the compiler • Consequences • More type errors are detected at compile-time • Programmer must type more text • Not as flexible: “You can't always do what you want” • Invocation of an action is faster • But this factor becomes less and less important...

  8. Supported? Yes/No?DT = Dynamic Typing; ST = Static Typing interface List { void add(Object o); } class ArrayList implements List { void add(Object o) { ... } } class LinkedList implements List { void addFirst(Object o) { ... } void add(Object o) { ... } } LinkedList w = new LinkedList(); w.addFirst("1"); // DT ST List x = new LinkedList(); x.addFirst("1"); // DT ST List y = new ArrayList(); y.addFirst("1"); // DT ST LinkedList z = new ArrayList(); // ST z.addFirst("1"); // DT

  9. What is a Type? • Theoretically a type is defined as a set of values • A more practical definition: A function that maps a value to either “yes”/”no” • The type is the set of all sources that are mapped to “yes” • The compiler usually represents types as a directed graph • Two types of nodes: Messages, Types • Edges are labeled (ordinal numbers)‏ • A type node has outgoing edges to all messages it can receive • A message node has outgoing edges to its return type, types of its parameters

  10. Type Checking: Statically Typed Language • Occurs when the compiler encounters an assignment • (Parameter passing is actually a sequence of assignments) void f(F f) { R r = f; // <- Type checking ...} • The idea: Compare the set of messages of F, R • In Java: • A subtype shares messages with its supertypes • The sets are compared for inclusion (not equality)‏

  11. Subtyping Schemes • Nominal subtyping: A type Y shares messages with X if Y declares X as a super type • A fixed set of super types • Structural conformance: A type Y shares messages with X if the messages have the same signature • One node for all messages of with the same signature • Regardless of where the messages are defined • An unlimited set of super types • This is actually a recursive definition

  12. Nominal vs. StructuralN = Nominal; S = Structural public interface R1 { public void a(); } public interface R2 { public void a(); } public interface F extends R1 { public void b(); } void g(F f) { R1 r1 = f; // N: S: R2 r2 = f; // N: S: ... }

  13. Typing Holes • Recall: A type system should prevent us from applying an operation to a value that does not support it • Is it really so? public class A { void f() { ... } } .. void g(A a) { a.f(); } • What values (of the variable a) will not support the operation a.f() ?

  14. Typing Strength • Strong typing: • An unbreakable association between a value and its type • Weak typing: • One can change the value-type association • Modern languages: Strong typing • With either static or dynamic type checking

More Related