Presentation is loading. Please wait.

Presentation is loading. Please wait.

Http://flic.kr/p/3aKUAq Data Types.

Similar presentations


Presentation on theme: "Http://flic.kr/p/3aKUAq Data Types."— Presentation transcript:

1 Data Types

2 Purpose of Types in PLs Provide implicit context
Example: a + b is integer addition if a and b are integers floating-point addition if a and b are floats etc. Limit operations (to prevent errors) Example: Prevent programmer from passing a string to a function that expects an integer

3 Parts of a Type System Mechanism to define types and associate them with constructs that have values Examples: constants, variables, parameters, subroutines Set of rules for Type equivalence Are two values of same type? Type compatibility Can value of this type be used in this context? Type inference What type is this expression, given the types of its parts?

4 Type Checking Process of ensuring program obeys type compatibility rules Strongly typed lang.: Prohibits invocation of any operation on any object that doesn’t support the operation Statically typed lang.: Strongly typed and type checking performed at compile time Many languages are mostly, but not entirely

5 Two Approaches to Type Equivalence
Structural equivalence: Use structure of objects Example of structurally equivalent types Name equivalence: Use names given by programmer More popular

6 What should you do if… … you want to use a value of one type in a context that requires a different type? C++ Example: float f = 5.5; int i = f % 2; // Error! Cast! int i = (int)f % 2;

7 The method could return a Foo or a class that is derived from Foo
Type Compatibility Most languages do not require equivalence in every context – just compatibility What are Java’s type compatibility rules? What compatible types could this method return? Foo myFooRef = someMethod(); The method could return a Foo or a class that is derived from Foo

8 Another Approach: Duck Typing
If it walks like a duck and swims like a duck and quacks like a duck, call it a duck JavaScript Example:

9 Type Coercion When value of one type used in context where another is expected, conversion of value to expected type May be trivial or may actually require computation Coercion controversial in lang. design because may lead to subtle errors Consider loss of precision coercing double to float

10 Universal Reference Type
Give programmer a way to reference any type void* in C/C++ Object in Java

11 Type Inference C++ Example: C# Example:
Types sometimes need to be inferred from expression: cout << x + y + z << endl; C# Example: Implicit type var: used as if you declared a type, but the compiler figures it out

12 Type Inference Another C# Example:

13 Activity: Java Type Checking
X x = new X(); Y y = new Y(); Z z = new Z(); X xy = new Y(); X xz = new Z(); Y yz = new Z(); Y y1 = new X(); Z z1 = new X(); X x1 = y; X x2 = z; Y y1 = (Y) x; Z z1 = (Z) x; Y y2 = (Y) x1; Z z2 = (Z) x2; Y y3 = (Y) z; Z z3 = (Z) y; Object o = z; Object o1 = (Y) o; Given Base class X Class Y extends X Class Z extends X For each statement, tell Which is involved? Type equivalence Type compatibility Type inference Static typing Dynamic typing What is the result? X Z Y

14 Discussion Question

15 Discussion Question

16 What’s next? Homework 3 due next class Exam 2 in one week


Download ppt "Http://flic.kr/p/3aKUAq Data Types."

Similar presentations


Ads by Google