- 3 -
Language basics Timeline Eiffel 11986 1988 (OOSC 1 st edition) - - PowerPoint PPT Presentation
Language basics Timeline Eiffel 11986 1988 (OOSC 1 st edition) - - PowerPoint PPT Presentation
- 3 - Language basics Timeline Eiffel 11986 1988 (OOSC 1 st edition) Eiffel 2 Eiffel 3 1990-1991 ( Eiffel: The Language ) Eiffel 4 1997 (Precursor mechanism, agents) ECMA 2005 ECMA/ISO 2006 (revised) http://www.ecma-
Timeline
Eiffel 11986 Eiffel 2 1988 (OOSC 1st edition) Eiffel 3 1990-1991 (Eiffel: The Language) Eiffel 4 1997 (“Precursor” mechanism, agents) ECMA 2005 ECMA/ISO 2006 (revised) http://www.ecma- international.org/publications/standards/ Ecma-367.htm See roadmap: http://eiffelsoftware.origo.ethz.ch/index.php/Language_road_map
Eiffel mechanisms
Classes, objects, ... Single and multiple inheritance Inheritance facilities: redefinition, undefinition, renaming Genericity, constrained and unconstrained Safe covariance Disciplined exception handling, based on principles of Design by Contract Full GC Agents (power of functional programming in O-O!) Unrestricted streaming: files, databases, networks...
What is not in Eiffel
- Goto
- Functions as arguments
- Pointer arithmetic
- Special increment syntax, e.g. x++, ++x
- In-class feature overloading
Syntax conventions
Semicolon used as a separator (not terminator) It’s optional almost all the time. Just forget about it! Style rules are an important part of Eiffel: Every feature should have a header comment Class should have an indexing clause Layout, indentation Choice of names for classes and features
Kinds of feature
Command Query Feature
Function
No result
Feature
Memory Computation
Client view (specification) Internal view (implementation)
Returns result
Attribute Procedure
Memory Computation
Routine Feature Feature
Uniform access principle
A call such as my_account. balance could use an attribute or a function
It doesn‘t matter to the client whether you look up or compute
Uniform Access
balance = deposits.total – withdrawals.total
deposits withdrawals balance deposits withdrawals
(A1) (A2)
Attributes (fields/ data members)
bounding_rectangle: RECTANGLE
- - Smallest rectangle including whole of current figure
Attributes with contracts (ECMA)
bounding_rectangle: RECTANGLE
- - Smallest rectangle including whole of current figure
require bounded attribute ensure Result.height = height Result.width = width Result.lower_left = lower_left Result.contains (Current) end
Self-initializing attributes (ECMA)
bounding_rectangle: FIGURE
- - Smallest rectangle including whole of current figure
- - (Computed only if needed)
require bounded attribute create Result.set (lower_left, width, height) ensure Result.height = height Result.width = width Result.lower_left = lower_left Result.contains (Current) end
… As before …
Another example: once per object
class STOCKS feature db: DATABASE history (ts: TICKER_SYMBOL): HISTORY
- - List of previous valuations of ts
attribute if {h: HISTORY} db.retrieved (ts) then Result := h else create Result
- - Produces empty list
end end end
A simple class
class POINT feature x, y: REAL
- - Point cartesian coordinates
move (a, b: REAL) is
- - Move by a horizontally and by b vertically.
do x := x + a y := y + b end scale (factor: REAL) is
- - Scale by factor.
do x := factor * x y := factor * y end
Class POINT (cont’d)
distance (p: POINT): REAL is
- - Distance to p
do Result := sqrt ((x – p.x)^2 + (y – p.y)^2) end ro: REAL is
- - Distance to origin (0, 0)
do Result := sqrt (x^2 + y^2) end theta: REAL is
- - Angle to horizontal axis
do … end end
Use of the class in a client (1/ 5)
class GRAPHICS feature p, q: POINT
- - Graphic points
… some_routine is
- - Use p and q.
local u, v: REAL do
- - Creation instructions
create p create q end end
0.0 0.0 p
(POINT)
0.0 0.0 q
(POINT)
Use of the class in a client (2/ 5)
class GRAPHICS feature p, q: POINT
- - Graphic points
… some_routine is
- - Use p and q.
local u, v: REAL do
- - Creation instructions
create p create q p.move (4.0, -2.0)
- - Compare with Pascal, C, Ada:
- - Move (p, 4.0, -2.0)
end end
4.0
- 2.0
p
(POINT)
0.0 0.0 q
(POINT)
Use of the class in a client (3/ 5)
class GRAPHICS feature p, q: POINT
- - Graphic points
… some_routine is
- - Use p and q.
local u, v: REAL do
- - Creation instructions
create p create q p.move (4.0, -2.0)
- - Compare with Pascal, C, Ada:
- - Move (p, 4.0, -2.0)
p.scale (0.5) end end
2.0
- 1.0
p
(POINT)
0.0 0.0 q
(POINT)
Use of the class in a client (4/ 5)
class GRAPHICS feature p, q: POINT
- - Graphic points
… some_routine is
- - Use p and q.
local u, v: REAL do
- - Creation instructions
create p create q p.move (4.0, -2.0)
- - Compare with Pascal, C, Ada:
- - Move (p, 4.0, -2.0)
p.scale (0.5) u := p.distance (q) v := p.x p := q end end
2.0
- 1.0
p
(POINT)
0.0 0.0 q
(POINT)
Use of the class in a client (5/ 5)
class GRAPHICS feature p, q: POINT
- - Graphic points
… some_routine is
- - Use p and q.
local u, v: REAL do
- - Creation instructions
create p create q p.move (4.0, -2.0)
- - Compare with Pascal, C, Ada:
- - Move (p, 4.0, -2.0)
p.scale (0.5) u := p.distance (q) v := p.x p := q p.scale (-3.0) end end
2.0
- 1.0
p
(POINT)
0.0 0.0 q
(POINT)
Example class
class SECURITY feature company_name: STRING nyse_ticker: STRING company: COMPANY traded: EXCHANGE latest_valuation: INTEGER public: BOOLEAN ... Other attributes and routines ... end
Creating an object
With the class SECURITY as given: my_security: SECURITY ...
create my_security
Effect of such a creation instruction: Allocate new object of the type declared for my_security. Initialize its fields to default values (0 for numbers, false for booleans, null for characters, void for references). Attach it to the instruction’s target, here my_security.
Specific creation procedures
class SECURITY create make_by_name, make_by_ticker, make_by_company feature -- Initialization make_by_name (n: STRING)
- - Initialize to company of name n.
do company_name := n ... end make_by_ticker (n: STRING) do ... end make_by_company (c: COMPANY) do ... end feature ... The rest as before ...
If there is a creation clause
Creation instructions must be “creation calls”, such as create my_security.make_by_name ("Microsoft") create my_security.make_by_ticker ("MSFT") create my_security.make_by_company (Microsoft_company)
If there is no creation clause
An absent creation clause, as in class SECURITY
- - No creation clause
feature … The rest as before … end is understood as one that would only list default_create, as in class SECURITY create default_create feature … The rest as before … end Procedure default_create is defined in ANY as doing nothing; any class can redefine it to provide proper default initializations.
To allow both forms
To make both forms valid: create my_security as well as create my_security.make_by_ticker ("MSFT") it suffices to make default_create (redefined or not) one of the creation procedures: class SECURITY create make_by_ticker, ..., default_create feature ... The rest as before ...
Associated convention
The notation
create x
is understood (if permitted) as an abbreviation for
create x.default_create
To prohibit instantiating a class
class NOT_CREATABLE create
- - Nothing here!
feature
... The rest as before ...
end
Creating an object of a specific type
If D is a descendant of A: a1: A … create {D} a1… Avoids local variables, as in d1: D … create d1…. a1 := d1 Particularly useful for factories
Types
Reference types; value of an entity is a reference Example: b: SECURITY Expanded types; value of an entity is an object. Example: d: expanded SECURITY
b
(SECURITY)
d
(SECURITY)
Expanded classes
You may declare a class as expanded class C ... The rest as usual ...
Subobjects
Expanded classes and entities support the notion of subobject
class RECTANGLE_R feature corner1, corner2, corner3, corner4: POINT ... end class RECTANGLE_E feature corner1, corner2, corner3, corner4: POINT_E … end
The meaning of expanded classes More than implementation notion: a system modeling tool. Two forms of client relation: Simple client Expanded client What is the difference between these two statements? A stock has a trading exchange A stock has an analyst valuation (Compare: a car has an originating factory and an engine)
Basic types as expanded classes
expanded class INTEGER ... expanded class BOOLEAN ... expanded class CHARACTER ... expanded class REAL ... expanded class DOUBLE ... n: INTEGER
Semantics of expanded types (ECMA)
An object has either reference or copy semantics
Infix and prefix operators (Eiffel 3)
expanded class INTEGER feature infix "+" (other: INTEGER): INTEGER is
- - Sum with other
do ... end infix "*" (other: INTEGER): INTEGER is
- - Product by other
do ... end prefix "–": INTEGER is
- - Unary minus
do ... end ... end Calls are then of the form i + j rather than i.plus (j).
Infix and prefix operators (ECMA)
expanded class INTEGER feature plus alias "+" (other: INTEGER): INTEGER is
- - Sum with other
do ... end multiplied alias "*" (other: INTEGER): INTEGER is
- - Product by other
do ... end minus alias "–": INTEGER is
- - Unary minus
do ... end ... end Calls are then of the form i + j rather than i.plus (j).
Effect of an assignment
Reference types: reference assignment Expanded types: value copy
3 class TWO_VALUES feature item: INTEGER right: TWO_VALUES set (n: INTEGER; r: TWO_VALUES) is
- - Reset both fields
do item := n right := r end end t: TWO_VALUES ... create t ... t.set (25, Void)
item := n right := r
Initial situation: Result of:
- b := a
- c := cloned (a)
- d := deep_cloned (a)
“Almaviva” name landlord loved_one a O1 “Figaro” O2 “Susanna” O3 b “Almaviva” O4 c “Almaviva” name landlord loved_one O5 “Figaro” O6 “Susanna” O7 d
Shallow and deep cloning
Where do these mechanisms come from? Class ANY in the Eiffel “Kernel Library” Every class that doesn’t explicitly inherit from another is considered to inherit from ANY As a result, every class is a descendant of ANY.
A related mechanism: Persistence
a.store (file) .... b ?= retrieved (file) Storage is automatic. Persistent objects identified individually by keys. These features come from the library class STORABLE.
The module-type merge
A class is both: A module A type Much of the conceptual power of the method comes from the fusion
- f these two notions.
From the module viewpoint: Set of available services (“features”). From the type viewpoint: Description of set of possible run-time objects (its instances). Connection: The services of the class, viewed as a module, are the
- perations applicable to the instances of the class, viewed as a
type.