1 / 16

Types and Type Equivalence in Programming Languages

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.

heidig
Télécharger la présentation

Types and Type Equivalence in Programming Languages

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. http://flic.kr/p/3aKUAq 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. 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

  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: • 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. 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

  14. Discussion Question

  15. Discussion Question

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

More Related