1 / 7

Survey of Common Types I

Survey of Common Types I. Records E.g., structs in C++ If elements are named, a record is projected into its fields (e.g., via struct member selection operator as in P.x in C++) Variant records (e.g., unions in C++) let different types overlap in memory Arrays

spencer
Download Presentation

Survey of Common Types I

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. Survey of Common Types I • Records • E.g., structs in C++ • If elements are named, a record is projected into its fields (e.g., via struct member selection operator as in P.x in C++) • Variant records (e.g., unions in C++) let different types overlap in memory • Arrays • Usually contiguous in memory and of a homogeneous type • Layout and access patterns also may affect cache effects, etc. • Provide an indexing operator (e.g., []) to access elements • May be allocated on stack or heap (language specific) • May be compatible with other types (e.g., pointers in C++)

  2. Survey of Common Types II • “Utility” types (e.g., strings, sets, lists, files) • Widely used but not necessarily inherent to a language itself • Sometimes built into the language (e.g., Pascal) • E.g., set operators (+, *, -) for union, intersection, and difference • Sometimes provided by a library (e.g., the C++ STL) • Strings • Often just an array of characters (e.g., C style char *) • Often an object with operators (e.g., C++ style string) • Sets (similarly, lists) • Contain unique values (or in the case of lists, ordered sequences) of a common type • Files • “Streams” of data coming from and/or going to mass storage

  3. Survey of Common Types III • Recursive types • May contain (an alias for) another instance of the same type • E.g., a list node contains an alias for the next node in the list • May require a pointer or other similar abstraction • To halt the nesting (and/or construction) of recursive type instances • Pointers and references • Similar in some languages, different in others • E.g., pointer/references semantics in Java vs. C++ • Also may be similar to other types • E.g., (const) pointer and array semantics in C++ • E.g., address arithmetic vs. index arithmetic in C++

  4. More about Type Constructors • Used to construct sets from other sets • E.g., as Cartesian products (tuples) • If elements are named, a record is projected into its fields (e.g., via struct member selection operator as in P.x in C++) • Specialized forms of type constructors are common • Unions often are at best unnecessary in object-oriented languages (or worse: in C++ certain examples can exhibit type problems similar to those seen with reinterpret casting) • Subset type constructors narrow a set (e.g., in Ada to construct a sub-range type from a range type) • Array constructors project an index type into another type • Pointer/reference constructors create aliases for other types (pointers have their own operators but references do not)

  5. Type Checking with Polymorphism • Hindley-Milner type checking • Strongly typed functional languages (ML and Haskell) do this • Unambiguous (deductive) type inference, by propagating constraints and info up and down an abstract parse tree • Relies on specific forms of instantiation and unification • Different types of polymorphism • Parametric (interface) polymorphism • Explicit subtype (inheritance) polymorphism • Liskov substitution principle: whenever S is a subtype of T, wherever you need an instance of T you can use one of S • Occurs-check in unification • Makes sure a variable isn’t in an expression substituted for it • Difficult to do efficiently, only use if necessary (it is for H-M)

  6. Explicit Polymorphism of Data • Relies on syntax to declare forms of polymorphism that cannot be handled implicitly as in previous cases • E.g., declaring polymorphic data structures • Essentially, they provide user-defined type constructors • E.g., in C/C++ function pointer and array typedefs • Explicit parametric polymorphism • Explicit declaration of type parameters (e.g., C++ templates) can be used to make data types generic over other types • In C++ calls to function templates are resolved implicitly but template objects require concrete type parameter declaration • Expressions within parameterized functions/methods impose constraints on the parameterized types (concept/models relation in generic programming, see Austern on C++ STL)

  7. Today’s Studio Exercises • We’ll code up ideas from Scott Chapter 7.3-7.9, 7.11 • Continuing our exploration of data types from last time • Today’s exercises are again in C++ • C++ allows us to explore many type system issues • Please take advantage of the on-line tutorial and reference manual pages that are linked on the course web site • As always, please ask us for help as needed • When done, email your answers with “Data Types Studio II” in the subject line, to the course account • Send to cse425@seas.wustl.edu

More Related