160 likes | 173 Views
Learn about the purpose of data types in programming languages, including providing implicit context and limiting operations to prevent errors. Explore type systems, type equivalence, type compatibility, type inference, and type checking in various languages.
E N D
http://flic.kr/p/3aKUAq Data Types
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
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?
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
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
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;
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 ora class that is derived from Foo
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:
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
Universal Reference Type • Give programmer a way to reference any type • void* in C/C++ • Object in Java
Type Inference • 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
Type Inference • Another C# Example:
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; Activity: Java Type Checking X • 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? Y Z
What’s next? • Homework 3 due next class • Exam 2 in one week