10 Conversion. Let’s start with conformance Conformance determines when a type may be used in lieu of another. Conformance relies on inheritance. The.

Slides:



Advertisements
Similar presentations
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
Advertisements

Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
ISBN Chapter 7 Expressions and Assignment Statements.
Kernighan/Ritchie: Kelley/Pohl:
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 7: SCOOP Type System (based on work with Piotr Nienaltowski)
1 Design by Contract Building Reliable Software. 2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification.
COMP3221: Microprocessors and Embedded Systems--Lecture 1 1 COMP3221: Microprocessors and Embedded Systems Lecture 3: Number Systems (I)
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
Chair of Software Engineering Piotr Nienaltowski, SCOOP Simple Concurrent Object-Oriented Programming.
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Chair of Software Engineering Piotr Nienaltowski, , Concurrent Object-Oriented Programming Bertrand Meyer, Piotr Nienaltowski.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
1 Advanced Material The following slides contain advanced material and are optional.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 6: Object Creation.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 6: Object Creation.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
ISBN Chapter 7 Expressions and Assignment Statements.
UML Class Diagrams: Basic Concepts. Objects –The purpose of class modeling is to describe objects. –An object is a concept, abstraction or thing that.
Variables, Constants, Methods, and Calculations Chapter 3 - Review.
Retrievals & Projections Objectives of the Lecture : To consider retrieval actions from a DB; To consider using relational algebra for defining relations;
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Object Oriented Programming
1-1 University of Hail College of Computer Science and Engineering Department of computer Science and Software Engineering Course: ICS313: Fundamentals.
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
C H A P T E R S E V E N Expressions and Assignment Statements.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
IT253: Computer Organization
Analyzing the Requirements with Formal Specifications Vienna Development Method Specification Language (VDM-SL) Book: Formal Software Development From.
Ad Hoc Constraints Objectives of the Lecture : To consider Ad Hoc Constraints in principle; To consider Ad Hoc Constraints in SQL; To consider other aspects.
The Data in a Relation To consider atomic data in relations; To consider data types in a relation; To consider missing data & NULLs in relations. Objectives.
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
CSI 3120, Expressions and assignment, page 1 Expressions and assignment Points for discussion Arithmetic expressions Overloading Logical expressions Assignment.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra.
Object-Oriented Design Simple Program Design Third Edition A Step-by-Step Approach 11.
Genericity Ranga Rodrigo Based on Mark Priestley's Lectures.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Arithmetic Expressions
Fixed & Floating Number Format Dr. Hugh Blanton ENTC 4337/5337.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
Operator Overloading Week 5.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Programming with Microsoft Visual Basic th Edition
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
Lecture 5 – Function (Part 2) FTMK, UTeM – Sem /2014.
Sequences Lecture 11. L62 Sequences Sequences are a way of ordering lists of objects. Java arrays are a type of sequence of finite size. Usually, mathematical.
Chapter 7: Expressions and Assignment Statements
Chapter 2 Elementary Programming
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 7: Expressions and Assignment Statements
Corky Cartwright January 18, 2017
Operator Overloading BCA Sem III K.I.R.A.S.
UML Class Diagrams: Basic Concepts
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Numerical Data Types.
College of Computer Science and Engineering
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
Chapter 7 Expressions and Assignment Statements.
Introducing JavaScript
Cardinality Definition: The cardinality of a set A is equal to the cardinality of a set B, denoted |A| = |B|, if and only if there is a one-to-one correspondence.
Once features.
PRESENTED BY ADNAN M. UZAIR NOMAN
Presentation transcript:

10 Conversion

Let’s start with conformance Conformance determines when a type may be used in lieu of another. Conformance relies on inheritance. The basic condition for V to conform to T is: The base class of V must be a descendant of the base class of T. t := v --t: T; v:V

Convertibility Convertibility completes the conformance mechanism. Convertibility lets you perform assignment and argument passing in cases where conformance does not hold but you still want the operation to succeed. Examples: your_real := 10-- your_real: REAL routine_expecting_a_real (10)

Implicit Conversion Most programming languages handle such cases through ad hoc rules applying to a fixed set of arithmetic types. But there is no reason to stop at INTEGER and REAL. With convertibility you can define a class COMPLEX that makes the assignment c := 10 valid for c of type COMPLEX, with the effect of calling a conversion to assign to c the complex number [10.0, 0.0].

Implicit Conversion Conversion is an abbrevation for an explicit form: your_real := Implicit is a shorthand for create your_real.from_integer (10) -- Explicit where class REAL_64 has a creation procedure from_integer that has been marked as a conversion procedure.

Conversion Basics expanded class REAL_64 inherit … create from_integer, from_real_32, … convert from_integer ({INTEGER}), from_real_32 ({REAL_32}) feature -- Initialization from_integer (n: INTEGER) -- Initialize by converting from n. do … Conversion algorithm end … end

Conversion Procedure, Conversion Type A procedure whose name appears in a Converters clause is a conversion procedure A type listed in a Converters clause is a conversion type. convert from_integer ({INTEGER}), from_real_32 ({REAL_32}) conversion types INTEGER, REAL_32convert to the current type REAL_64.

Conversion Procedure, Conversion Type With conversion procedures you permit assignments and argument passing from the given conversion types to the current type. This justifies mixed-type arithmetic expressions like: your_real + your_integer Since that notation is really a shortcut for a function call your_real.plus (your_integer)

Conversion Queries Conversion procedures allow conversions from U (to T). convert -- in class REAL_64 from_integer ({INTEGER}) Conversion queries specify conversions to T (from U). convert -- in class INTEGER to_real_64: {INTEGER} If you have access to both T and U, you can use either mechanism. But sometimes you only have access to T or U.

Conversion Queries: Example In class STRING we can provide a conversion procedure as well as a function going the other way: from_other (s: OTHER_STRING) -- Initialize from s. to_other: OTHER_STRING -- Representation in “other” form of current string.

Conversion Queries: Example Assume that eiffel_routine expects an Eiffel STRING and external_routine expects an OTHER_STRING. We could either use explicit transformations: eiffel_string: STRING external_string: OTHER_STRING … eiffel_routine (create s.from_other (external_string)) external_routine (s.to_other) or implicit transformations: eiffel_routine (external_string) external_routine (s)

Conversion Queries: Example In the implicit case, if restricted to conversion procedures, you would need to add a conversion procedure to OTHER_STRING. This won’t work if OTHER_STRING is an external class over which you have no control. You can work from the other side in such cases - by marking to_other as a conversion query: create from_other convert from_other ({OTHER_STRING}), to_other: {OTHER_STRING}

Using Conversion Properly You never have to include a conversion facility. You can either use the explicit form: create target.conversion_procedure (source) Or make use of the conversion facility: target := source A general advice is: A creation procedure should provide a conversion mechanism only if the associated operation does not entail any loss of information. real := integer-- OK integer := real-- Information loss integer := real.truncated-- OK (explicit)

Conversion Principles  Conversion Principle: No type may both conform and convert to another.  Conversion Asymmetry Principle: No type T may convert to another through both a conversion procedure and conversion query.  Conversion Non-Transitivity Principle That V converts to U and U to T does not imply that V converts to T.

Conversion Principle When you read x := y You see immediately which of convertibility or conformance applies:  If y conforms to x, because TY and TX are the same or there is direct inheritance between their base classes, there is no conversion involved.  If the class texts specify that TY converts to TX, the attachment will involve a conversion. If an attachment involves a conversion, it’s always because the types don’t conform.

Conversion Asymmetry Principle Ensures that a conversion through a procedure cannot compete with a conversion through a query. from_string (s: STRING) -- in class OTHER_STRING to_other: OTHER_STRING -- in class STRING my_other_string := my_string Which conversion feature will be used?

Conversion Non-Transitivity Principle Convertibility is always the result of an explicit convert clause. Conversion involves a transformation of values, which should always be explicit. With transitivity, additional transformations would occur behind the scene. Therefore convertibility is non-transitive.

Explicit Conversion Conversion usually happens implicitly as a result of a reattachment: var := exp-- var: T, exp:U Additionally there is a library feature that explicitly implies a conversion: {T} [exp] If U converts to T, this expression denotes the result of converting exp to T, and is called an explicit conversion.

Explicit Conversion Not a language mechanism to achieve this: The Kernel Library class TYPE [G] provides a function adapted alias “[]” (x: G): G which can be used for any type T and any expression exp of a type U compatible with T to produce a T version of exp. {T} [exp] is equivalent to t_type.adapted (exp)-- t_type: TYPE [T] Where {T} represents TYPE [T]

Explicit Conversion: Example {DATE} [[20, “April”, 2007]] The tuple is given as argument to the adapted function of {DATE} that turns it into an expression of type DATE. This is permitted if class DATE specifies a conversion procedure from TUPLE [NATURAL, STRING, NATURAL] to DATE.

Explicit Conversion: Example compute_revenue (a: DATE; b: DATE) compute_revenue ([1, ”January”, 2007], [31, “May”, 2007]) Where compute_revenue is expecting two arguments of type DATE works also without specifying {DATE}. The need for explicit conversion arises because conversion is not transitive.

Explicit Conversion: Example process_date_string (s: STRING) Assume that DATE converts to STRING and the conversion from tuple to DATE is still valid: process_date_string ([1, “January”, 2007]) is invalid as it requires two conversions. process_date_string ({DATE} [[1, “January”, 2007]]) is valid as it has only one implicit conversion. It is possible to specify several conversions in this style: {T} [{U} [of_type_V]]

Target Conversion my_integer + my_real my_integer.plus (my_real) We don’t want to use feature plus of class INTEGER, but convert my_integer to REAL and then use feature plus of class REAL. The target conversion mechanism makes this possible. It is supported syntactically by the optional convert mark of an Alias for an infix feature: plus alias “+” convert (other: INTEGER): INTEGER

Target Conversion plus alias “+” convert (other: INTEGER): INTEGER The convert mark means: If the type of the argument is not the normally expected argument type, but one to which the current type converts, such as REAL: 1.Convert the target to the applicable conversion type 2.Ignore the implementation here (integer addition) 3.Instead call the function with the same name from the applicable conversion type (here REAL)

Target Conversion: Discussion Purpose to support traditional conventions of mathematical notation, in particular mixed-type arithmetic expressions. Given that this is the usual expectation for numerical computation, four approaches are possible in an object-oriented language:

Target Conversion: Discussion 1.Ignore the issue: Treat arithmetic types as completely different from O-O types. (Done in Java, C++) 2.Insist on purity: Mixed-type expressions are wrong and one should use explicit conversion. (Programmers expect be able to use mixed-type expressions) 3.Provide special rules for basic types: Keep integers, reals and such within the O-O type system. Introduce special cases in the conformance and reattachment rules. (Used in Eiffel 3, couldn’t handle expressions like my_integer + my_real) 4.Provide a general conversion mechanism: As seen here.

End of Lecture 10