Http://flic.kr/p/3aKUAq Data Types.

Slides:



Advertisements
Similar presentations
CS7100 (Prasad)L16-7AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
Advertisements

Chapter 7:: Data Types Programming Language Pragmatics
Type Checking.
Numeric literals and named constants. Numeric literals Numeric literal: Example: A numeric literal is a constant value that appears in a Java program.
Variables Names Bindings Type Scope. L-Value versus R-Value Not complicated Associated with assignment statements Left hand side represents an address.
Overloading methods review When is the return statement required? What do the following method headers tell us? public static int max (int a, int b)
CS102 Data Types in Java CS 102 Java’s Central Casting.
Computer Science 1620 Function Scope & Global Variables.
Multiple-Subscripted Array
Expressions creating information. topics  operators: precedence, associativity, parentheses, overloading  operands: side-effects, coercion  arithmetic,
Names and Binding In procedural programming, you write instructions the manipulate the “state” of the process where the “state” is the collection of variables.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Names Variables Type Checking Strong Typing Type Compatibility 1.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
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.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Objects First With Java A Practical Introduction Using BlueJ Casting Week
Data Types (3) 1 Programming Languages – Principles and Practice by Kenneth C Louden.
Hindley-Milner Type Inference CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Names, Scope, and Bindings Programming Languages and Paradigms.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
April 13, 1998CS102-02Lecture 3-1 Data Types in Java CS Lecture 3-1 Java’s Central Casting.
Chapter 5 Names, Bindings, Type Checking CSCE 343.
Heath Carroll Bill Hanczaryk Rich Porter.  A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the.
Design issues for Object-Oriented Languages
Arithmetic Expressions Function Calls Output
Chapter 7: Expressions and Assignment Statements
Semantic Analysis Type Checking
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Principles of programming languages 8: Types
Lecture 4: Type Systems.
Lecture 16: Introduction to Data Types
CS 326 Programming Languages, Concepts and Implementation
Representation, Syntax, Paradigms, Types
Chapter 7: Expressions and Assignment Statements
Chapter 5 Function Basics
Pointers and Pointer-Based Strings
Type Conversion, Constants, and the String Object
Methods and Parameters
Chapter 2.
Conversions of the type of the value of an expression
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Variables and Their scope
CS 3304 Comparative Languages
Type Systems CSE 340 – Principles of Programming Languages Fall 2016
Representation, Syntax, Paradigms, Types
Manipulating Pictures, Arrays, and Loops part 3
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Representation, Syntax, Paradigms, Types
CS 363 – Chapter 7 Chapter 7 – type systems Types that are supported
Names and Binding In Text: Chapter 5.
Representation, Syntax, Paradigms, Types
Pointers and Pointer-Based Strings
Compiler Construction
Java’s Central Casting
Classes, Objects and Methods
CSCE 314: Programming Languages Dr. Dylan Shell
Subtype Substitution Principle
Compiler Construction
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Type Systems.
Presentation transcript:

http://flic.kr/p/3aKUAq Data Types

Purpose of Types in PLs Provide implicit context Example: a + b is integer addition if a and b are integers floating-point addition if a and b are floats etc. Limit operations (to prevent errors) Example: Prevent programmer from passing a string to a function that expects an integer

Parts of a Type System Mechanism to define types and associate them with constructs that have values Examples: constants, variables, parameters, subroutines Set of rules for Type equivalence Are two values of same type? Type compatibility Can value of this type be used in this context? Type inference What type is this expression, given the types of its parts?

Type Checking Process of ensuring program obeys type compatibility rules Strongly typed lang.: Prohibits invocation of any operation on any object that doesn’t support the operation Statically typed lang.: Strongly typed and type checking performed at compile time Many languages are mostly, but not entirely

Two Approaches to Type Equivalence Structural equivalence: Use structure of objects Example of structurally equivalent types Name equivalence: Use names given by programmer More popular

What should you do if… … you want to use a value of one type in a context that requires a different type? C++ Example: float f = 5.5; int i = f % 2; // Error! Cast! int i = (int)f % 2;

The method could return a Foo or a class that is derived from Foo Type Compatibility Most languages do not require equivalence in every context – just compatibility What are Java’s type compatibility rules? What compatible types could this method return? Foo myFooRef = someMethod(); The method could return a Foo or a class that is derived from Foo

Another Approach: Duck Typing If it walks like a duck and swims like a duck and quacks like a duck, call it a duck JavaScript Example:

Type Coercion When value of one type used in context where another is expected, conversion of value to expected type May be trivial or may actually require computation Coercion controversial in lang. design because may lead to subtle errors Consider loss of precision coercing double to float

Universal Reference Type Give programmer a way to reference any type void* in C/C++ Object in Java

Type Inference C++ Example: C# Example: Types sometimes need to be inferred from expression: cout << x + y + z << endl; C# Example: Implicit type var: used as if you declared a type, but the compiler figures it out

Type Inference Another C# Example:

Activity: Java Type Checking X x = new X(); Y y = new Y(); Z z = new Z(); X xy = new Y(); X xz = new Z(); Y yz = new Z(); Y y1 = new X(); Z z1 = new X(); X x1 = y; X x2 = z; Y y1 = (Y) x; Z z1 = (Z) x; Y y2 = (Y) x1; Z z2 = (Z) x2; Y y3 = (Y) z; Z z3 = (Z) y; Object o = z; Object o1 = (Y) o; Given Base class X Class Y extends X Class Z extends X For each statement, tell Which is involved? Type equivalence Type compatibility Type inference Static typing Dynamic typing What is the result? X Z Y

Discussion Question

Discussion Question

What’s next? Homework 3 due next class Exam 2 in one week