Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.

Similar presentations


Presentation on theme: "CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language."— Presentation transcript:

1 CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language or a standard may define representations –E.g., IEEE floating point standard, sizeof(char) == 1 in C++ Otherwise representations may be platform-dependent –E.g., different int sizes on 32-bit vs. 64-bit platform in C++ Languages also define operations on data –E.g., strong requirements for arithmetic operators in Java –E.g., weaker requirements for them in C++ Languages also differ in when types are checked –E.g., Scheme is dynamically type checked (at run-time) –E.g., C++ is (mostly) statically checked at compile-time

2 CSE 425: Data Types I Programs and Type Systems A language’s type system has two main parts –Its type constructors –Its rules (and algorithms) for evaluating type equivalence, type compatibility, and type inference Type systems must balance design trade-offs –Static checking can improve efficiency, catch errors earlier –Dynamic checking increases flexibility by delaying type evaluation until run-time (common in functional languages), may consider a larger subset of safe programs to be legal Definition of data types (based on sets) –A data type has a set of values and a type name –A data type also defines operations on those values, along with properties of those operations (e.g., integer division)

3 CSE 425: Data Types I Predefined Types, Simple Types Languages often predefine types –Simple types (e.g., int and double in C++ or Java) have only a basic mathematic (arithmetic or sequential) structure –Other types with additional structure may be predefined in the language (e.g., the String type in Java) Languages also often allow types to be introduced –Simple types can be specified within a program (e.g., an enum in C++ or a sub-range type in Ada) –Other types (e.g., unions, arrays, structs, classes, pointers) can be declared explicitly but involve type constructors

4 CSE 425: Data Types I Type Equivalence Structural equivalence –Basic structural equivalence: all types defined as AXB are the same but are all different than those defined as BXA –More complex forms arise if elements are named (so they are not interface polymorphic to member selection operator) Name equivalence –Two types are the same only if they have the same name –Easy to check, but restrictive Declaration equivalence –Improves on name equivalence by allowing new names to be given for a declared type, all of which are equivalent (e.g., in C++ typedefs behave this way)

5 CSE 425: Data Types I Type Checking Some languages check statically but weakly –E.g., C++ warnings rather than errors for C compatibility Type compatibility in construction and assignment –Variable constructed, or l-value/ref assigned from an r-value –May involve conversion of r-value into a compatible type Overloading introduces inference in type checking –Also may involve type conversion of operands –E.g., passing an int to a long int parameter Casting overrides type checking –E.g., cannot assign long int result back into a short int in Java unless you tell the compiler explicitly to recast r-value –E.g., C++ reinterpret cast tells compiler not to check

6 CSE 425: Data Types I Type Conversion Often need to use one type in place of another type –Safe implicit conversions often are made automatically –Widening conversions usually don’t lose information –Narrowing conversions may truncate, overflow, etc. Explicit conversions –Explicit construction from a type provides safe conversion –Casting offers alternative when safety can only be determined at run-time (by the program doing the cast) –Static casts are completed at compile-time –Dynamic casts check additional information at run-time (e.g., if polymorphic) may throw exception, return 0 pointer, etc.

7 CSE 425: Data Types I Today’s Studio Exercises We’ll code up ideas from Scott Chapter 7.1-7.2, 7.10 –Especially looking at types, type declarations, casting Today’s exercises are all in C++ –Scheme, which we’ll work with later in the course as well, is dynamically (rather than statically) typed –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 I” in the subject line to the course account –Send to cse425@seas.wustl.edu


Download ppt "CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language."

Similar presentations


Ads by Google