programming language concepts data types · 2018. 8. 27. · examples of data types - primitive...

27
Programming Language Concepts Data Types Janyl Jumadinova 9 February, 2017

Upload: others

Post on 09-Feb-2021

14 views

Category:

Documents


0 download

TRANSCRIPT

  • Programming Language ConceptsData Types

    Janyl Jumadinova9 February, 2017

  • Examples of Data Types

    - Primitive data types- Reference data types

    - ADT (abstract data types)

    2/18

  • Examples of Data Types

    - Primitive data types- Reference data types

    - ADT (abstract data types)

    2/18

  • Data Types

    I What are types good for?- implicit context- checking- make sure that certain meaningless operations do not occur

    I Type checking cannot prevent all meaningless operations

    I It catches enough of them to be useful

    I Polymorphism results when the compiler finds that it doesn’tneed to know certain things

    3/18

  • Data Types

    I What are types good for?- implicit context- checking- make sure that certain meaningless operations do not occur

    I Type checking cannot prevent all meaningless operations

    I It catches enough of them to be useful

    I Polymorphism results when the compiler finds that it doesn’tneed to know certain things

    3/18

  • Data Types

    I What are types good for?- implicit context- checking- make sure that certain meaningless operations do not occur

    I Type checking cannot prevent all meaningless operations

    I It catches enough of them to be useful

    I Polymorphism results when the compiler finds that it doesn’tneed to know certain things

    3/18

  • What Does “Implicit Context” Mean?

    When we see a statement such as: x = y + z;are we:

    I adding two int values, storing in an int?

    I adding two int values, storing in a double?

    I concatenating a string and an int, storing in a string

    I adding an int and a double, storing in a double?

    4/18

  • What Does “Implicit Context” Mean?

    I If we were writing machine code, WE WOULD HAVE TOSPECIFY THIS(e.g., we would have to explicitly convert int to double beforeadding to a double or storing as a double; we would have toreserve space for the new string; etc.).

    I Type information gives the compiler or interpreter a contextthat enables it to figure this out

    5/18

  • What is Polymorphism?

    I In this context we are concerned with situations when the samevariable can refer, at different times, to values of different types.

    I The most familiar example to Java programmers occurs insubclasses.

    6/18

  • Polymorphism

    7/18

  • Polymorphism

    In Java, a subclass cannot override an instance variable of the parentclass; however, it can “shadow it”.On the other hand, methods CAN be overridden.

    8/18

  • Data Types

    STRONG TYPING has become a popular buzz-word

    – like structured programming– informally, it means that the language prevents you from applyingan operation to data on which it is not appropriate

    STATIC TYPING

    means that the compiler can do all the checking at compile time

    9/18

  • Data Types

    STRONG TYPING has become a popular buzz-word

    – like structured programming– informally, it means that the language prevents you from applyingan operation to data on which it is not appropriate

    STATIC TYPING

    means that the compiler can do all the checking at compile time

    9/18

  • Type Systems: Examples

    I Common Lisp is strongly typed, but not statically typed

    I Ada is statically typed

    I Pascal is almost statically typed

    I Java is strongly typed, with a non-trivial mix of things that canbe checked statically and things that have to be checkeddynamically

    10/18

  • Type Systems: Common Terms

    I Discrete types - countableinteger, boolean, char, enumeration, subrange

    I Scalar types - one-dimensional

    I Composite typesrecords (unions), arrays, sets, pointers, lists, files

    11/18

  • Type Systems: Common Terms

    I Discrete types - countableinteger, boolean, char, enumeration, subrange

    I Scalar types - one-dimensional

    I Composite typesrecords (unions), arrays, sets, pointers, lists, files

    11/18

  • Type Systems: Common Terms

    I Discrete types - countableinteger, boolean, char, enumeration, subrange

    I Scalar types - one-dimensional

    I Composite typesrecords (unions), arrays, sets, pointers, lists, files

    11/18

  • Composite types

    I Several values of varying types under a common name.

    I Made somewhat obsolete by classes and instances inobject-oriented programming.

    struct rec { /* here we declare the type */

    int i; double x; char s[10];

    };

    struct rec a,b,c; /* declare variables */

    a.i = 10; b.x = 4.14

    12/18

  • Composite types

    I Several values of varying types under a common name.

    I Made somewhat obsolete by classes and instances inobject-oriented programming.

    struct rec { /* here we declare the type */

    int i; double x; char s[10];

    };

    struct rec a,b,c; /* declare variables */

    a.i = 10; b.x = 4.14

    12/18

  • Composite types: Unions

    Several values of varying types under a common name and sharingthe same memory.

    union share { /* here we declare the type */

    int i; double x; char s[10];

    };

    union share a,b,c; /* declare variables */

    a.i = 10; /* this changes a.x and a.s also */

    13/18

  • Composite types: Enumerated types

    Symbolic names (actual underlying values not important).

    enum weekday {mon,tue,wed,thu,fri,sat,sun};

    enum weekday d;

    d = mon;

    if (d < fri) ...

    14/18

  • Composite types: Subranges

    I Lets you put bounds on the allowed values for certain variables.

    I E.g., if we really want an integer variable to hold only valuesbetween 1 and 31, we can create a subrange type. (Pascal andAda both have this.)

    Example (Ada):

    subtype Day is Integer range 1..31;

    ...

    d: Day; -- d may hold only values 1,...,31

    15/18

  • Type Systems

    A collection of features is orthogonal if there are no restrictionson the ways in which the features can be combined

    ORTHOGONALITY is a useful goal in the design of a language,particularly its type system

    16/18

  • Orthogonality Example

    Pascal is more orthogonal than Fortran, (because it allows arrays ofanything, for instance), but it does not permit variant records asarbitrary fields of other records (for instance).

    Orthogonality is nice primarily because it makes a language easy tounderstand, easy to use, and easy to reason about

    17/18

  • Orthogonality Example

    Pascal is more orthogonal than Fortran, (because it allows arrays ofanything, for instance), but it does not permit variant records asarbitrary fields of other records (for instance).

    Orthogonality is nice primarily because it makes a language easy tounderstand, easy to use, and easy to reason about

    17/18

  • Type Checking

    A TYPE SYSTEM has rules for:

    I type equivalence (when are the types of two values the same?)

    I type compatibility (when can a value of type A be used in acontext that expects type B?)

    I type inference (what is the type of an expression, given thetypes of the operands?)

    18/18

  • Type Checking

    A TYPE SYSTEM has rules for:

    I type equivalence (when are the types of two values the same?)

    I type compatibility (when can a value of type A be used in acontext that expects type B?)

    I type inference (what is the type of an expression, given thetypes of the operands?)

    18/18