CSC302 2006S, Class 26: Types (2) - Type Musings Admin: * Reading to be distributed later today. Sorry. * Cool talk today at 4:30. EC for attending. * Late: Dimitar Overview: * Ways to think about types, revisited * Ways to combine types, revisited * Pointer types * Notes on Liskov /Ways to think about types/ * Mappings from underlying bits to values (abstract) * Sets of values * Restrictions on how they can be used * Descriptions of capabilities /Ways to represent types/ * Primitive types (by language; by programmer): Emphasize sets * Combine types, e.g. records/crossproduct; arrays/sequences: Emphasize sets * Interfaces: Emphasize capabilities public interface Addable { public T add(T other); } Addable x; public class Foo implements Addable { ... } /Set perspective and relationship to above/ * Common set operations * Set product - x, records, arrays * Powerset - arrays, sequences * Function - funtions * Union * Intersection * Difference (complement) * Do any language constructs support type union, intersection, difference? * Why would we want to build such a type? * Language constructs to support union * Why? * X can be of type A or of type B? * Sort can be applied to multiple kinds of values * Overloading * Polymorphism * Conserve memory * Type conversion * How? * tagcase in CLU * Perl - Every value is a union of almost every type * Pascal has "variant records" * C has a union type * C has "pointer" as general union * Langauge constructs to support intersection * Why? * We might want to indicate that X has the attributes of A and the attributes of type B. For example, we might want something that is both addable and drawable. * How? * Multiple inheritance (of interfaces or classes) public interface AD extends Drawable,Addable * Set difference (e.g., "All addable objects that are not multipliable" or "reals that are not also integers") * Why? * How? * Sam's view: Not generally in languages /Beyond Set Theory: Other PL Types/ * Pointer/Reference * Do you support pointers/references as "obvious" types? * Why permit? * Gives programmer complete control over allocation and deallocation and sharing * If the language only supports pass-by-value, pointers permit you to simulate pass-by-reference * Pass-by-reference can be more efficient * Self-modifying code * Why disallow? * Programmers get things wrong. Memory leaks. Unintentional aliasing. Etc. * Can overwrite parts of the system (effects of aliasing) * What operations do you expect to have when you have pointers? * Dereference (get value pointed to) and reference (get pointer to) * Allocate and deallocate memory to point to. * Pointer manipulation - Addition, subtraction, choosing where to point to xp += 2; xp -= 2; xp = (int *) 1023123; while (*a++ = *b++) ;