what are types
play

What are Types? Denotational: Collection of values from domain - PDF document

What are Types? Denotational: Collection of values from domain CSCI: 4500/6500 Programming integers, doubles, characters Languages Abstraction: Collection of operations that can be applied to entities of that type Structural:


  1. What are Types? � Denotational: Collection of values from domain CSCI: 4500/6500 Programming » integers, doubles, characters Languages � Abstraction: Collection of operations that can be applied to entities of that type � Structural: Internal structure of a bunch of data, described down to the level of a small set of Types fundamental types � Implementers: Equivalence classes 1 2 Maria Hybinette, UGA Maria Hybinette, UGA Different Definitions: Types versus Typeless What are types good for? � Documentation: Type for specific purposes documents 1. "Typed" or not depends on how the interpretation of the program data representations is determined » Example: Person, BankAccount, Window, Dictionary » When an operator is applied to data objects, the � Safety: Values are used consistent with their types. interpretation of their values could be determined either » Limit valid set of operation by the operator or by the data objects themselves » Type Checking: Prevents meaningless operations (or » Languages in which the interpretation of a data object is catch enough information to be useful) determined by the operator applied to it are called � Efficiency: Exploit additional information typeless languages; » Example: a type may dictate alignment at at a multiple » those in which the interpretation is determined by the data object itself are called typed languages. of 4, the compiler may be able to use more efficient machine code instructions 2. Sometimes it just means that the object does not need � Abstraction: Types allow programmer to think about to be explicitly declared (Visual Basic, Jscript … ). programs at a higher level � Polymorphism: when the compiler finds that it does not need to know certain things. 3 4 Maria Hybinette, UGA Maria Hybinette, UGA What is type again? Weakly typed and Weak typing Weakly typed: Can successfully perform an improper Looking at an object: � operation to an object � Syntactically compatible: The object provides Type Weak typing (latent typing): Type constraints are all the expected operations (type names, � relaxed to make programming more flexible and powerful function signatures, interfaces) (“no type at all typing”) � Semantically compatible: The object's la·tent Class operations all behave in the expected way (state 1. Present or potential but not evident or active. In existence but semantics, logical axioms, proofs) not manifest. Latent talent. 2. Psychology. Present and accessible in the unconscious mind but not consciously expressed. � Type: it provides a particular interface » Example: Duck typing: if it walks like a duck … » Does not imply type safety or unsafety (later) � Class: describes implementation constraints 5 6 Maria Hybinette, UGA Maria Hybinette, UGA

  2. Weak Type & Type Safety Weak Type & Type Safety � Weak typing implicitly/automatically convert (or allow � Weak typing implicitly/automatically convert (or allow cast) cast) when used. when used. int x = 5; var x = 5; // (1) var x = 5; // (1) char y[] = “hi”; var y = “hi” // (2) var y = “4” // (2) char *z = x + y; x + y ; // (3) x + y ; // (3) � z points to a memory address 5 character beyond y (pointer � Visual Basic (pre.net) arithmetic). » Content of that location may be outside addressable memory » Left yields “5hi” (dereferencing z may terminate program) » Right yields? 9 or “54”? » Unsafe � Also program does not crash so in this respect Visual Basic appears “type-safe” 7 8 Maria Hybinette, UGA Maria Hybinette, UGA Static versus Dynamic Type Strongly and Weakly Typed Checking � Strongly typed typed languages prevents you from � Static type checking: check constraints at applying an operation on data that is inappropriate compile time. » Ada, ML, Haskell � Dynamic type checking: check constraints at » Example of not strongly typed: C’s cast operation: forces run time compiler to accept and allow the conversion at run-time even if it is in appropriate » Examples: Java, constrains the types and usage of � Weakly typed languages: can successfully perform and all the symbols at compile time, but also has improper operation to an object (un-safe) enough runtime support to perform limited dynamic checks. If it can't check something statically (array � Weak (latently) typed languages (different bounds or null pointers, for example), it checks polymorphism) automatically convert (or casts) types them dynamically. when used » Visual Basic (pre-.Net) 9 10 Maria Hybinette, UGA Maria Hybinette, UGA Example: Static and Dynamic Type Checking Example Implementation var x = 5; // (1) var x; // (1) var y := “hi”; // (2) x := 5; // (2) var z = x + y; // (3) x := “hi” // (3) � (1) binds 5 to x (2) binds “hi” to y; and (3) � (1) declares the name x (2) associates integer attempts to add x to y. value to x and (3) associates string value “hi” to x � Binding may be represented by pairs » Most statically typed languages illegal because (2) » (integer, 5) and (string, “hi”) and (3) bind x to values of inconsistent types � Executing line 3 … error… » Pure dynamically typed languages would permit it because the name x would not have to have consistent type. – Dynamic languages catch errors related to misuse of values “type errors” at the time of the computation of the statement/expression. 11 12 Maria Hybinette, UGA Maria Hybinette, UGA

  3. Advantages of Static Typing Implicit and Explicit Types � More reliable [ ? ] � Explicit (Manifest) Typing: Types appears as syntax in the program � more efficient at run time [ yes! ] » int a, char b; C++/C � Implicit Typing: Can infer type, not part of the text of the program » ML and Scheme 13 14 Maria Hybinette, UGA Maria Hybinette, UGA Type Equivalence Structural Equivalence struct student { string name; string address;} � Determining when the types of two values are struct school { string name; string address;} the same � The same component, put together the same struct student { string name; string address;} struct school { string name; string address;} » Algol, early Pascal, C (with exception) and ML � Straightforward and easy to implement � Are these the same? � Definition varies from language to language » Does ORDER of the fields matter � Yes! Example is structurally equivalent 15 16 Maria Hybinette, UGA Maria Hybinette, UGA Strict & Loose Name Name (Nominative) Equivalence Equivalence struct student { string name; string address;} � Aliasing: Multiple names for the same type struct school { string name; string address;} » typdef in C/C++ » TYPE new_type = old_type in Modula 2 � More popular recently � Subtyping: explicit naming of parent � A new name or definition = a new type » Java, current Pascal, Ada � Assume that: if programmer wrote two � Strict: aliases are distinct types different definitions, then wanted two types � Loose: aliases are equivalent types » Is this a good or bad assumption to make? � Example: No! not name equivalent 17 18 Maria Hybinette, UGA Maria Hybinette, UGA

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend