Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 363 – Chapter 7 Chapter 7 – type systems Types that are supported

Similar presentations


Presentation on theme: "CS 363 – Chapter 7 Chapter 7 – type systems Types that are supported"— Presentation transcript:

1 CS 363 – Chapter 7 Chapter 7 – type systems Types that are supported
scalar composite user-defined Some issues with types equivalence, compatibility

2 Why types? What do these Java expressions mean:
a + b Math.sqrt(fileMenu) Some languages are typeless How does it work? Better or worse to have types?

3 Strong typing Languages (compilers) vary in how strong their typing rules are. Strong typing means: whenever you use a expression, its type must be appropriate for the context x : integer; y : real; x := y; (* should we allow this? *)

4 Thinking about types 3 ways to view types
Denotational – a set of values Making sure that integer division stays closed Constructive – a type is represented as a built-in type or a composite of built-in types Abstract – focus on the operations we want to perform (In OO design, we do a lot of constructive and abstract thinking.)

5 Numeric types Integral types Real number types Integers Boolean
signed, unsigned, BCD Most languages don’t specify size  Boolean Character Real number types Floating point: universal standard Fixed point: no exponent; rare

6 Enum type Our own type with names for each value
Ex. days of the week, months of year type weekday = (mon, tue, wed, thur, fri) ; Internally represented as integer Pascal: pred() and succ() functions compatible with integers ? Pascal: no C: yes, can use a cast

7 Subranges If you have a variable, and you know it will only take on a small subset of values type test_score = ; type year = ; type coef is digits 7 range -1e10..+1e10; Usually not compatible with superset type. Advantages?

8 Base vs. composite types
Base types Integers Characters Boolean Real numbers Enum subranges Composite types record union array pointer list set file Some of these composite (derived) types are “collections” that we’ve already seen

9 Type stuff Restrictions and freedom Compatible types

10 Limits on types Some languages are more limited in type choices.
Algol: int, real, boolean, with arrays. How do you program without records/classes? What if array is the only way to aggregate?

11 Freedom Orthogonality = being able to do what you want regardless of type. For example: declare variables in any order give any name I want to variable can type a constant value of any type function can return value of any type can print any type can store any type in an array Languages usually have some limitations regarding orthogonality. Ex. “++”

12 Compatible types According to name According to structure
When is it legal to assign a := b or pass as matching parameter? According to name Both variables must be declared to be literally the same type easy for compiler to check According to structure Both types have the same “structure” Ex. arrays of 100 integers

13 Structure examples type fahrenheit = float; type celsius = float; type array = [0..9] of int; type vector= [0..9] of int; // anonymous types: a : [0..9] of celsius; b : [1..10] of celsius; c : [1..10] of float; According to structure equivalence, these types should match.

14 Which is better? Usually, languages go with name equivalence, and allow loopholes: coercion casting identically declared types the same: “loose name equivalence” subranges and integers If programmer defined a distinct type, there’s probably a reason! Structural equivalence would require recursive algorithm for record/classes 

15 Expression type When an expr combines variables of diff types, type of overall expr is usually: Promotion according to a hierarchy Ex. double, float, unsigned long, long, unsigned, int, short The base type, if different subtypes Ex. (0..20) + ( )  int


Download ppt "CS 363 – Chapter 7 Chapter 7 – type systems Types that are supported"

Similar presentations


Ads by Google