Language basics Timeline Eiffel 11986 1988 (OOSC 1 st edition) - - PowerPoint PPT Presentation

language basics timeline
SMART_READER_LITE
LIVE PREVIEW

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-


slide-1
SLIDE 1
  • 3 -

Language basics

slide-2
SLIDE 2

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

slide-3
SLIDE 3

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...

slide-4
SLIDE 4

What is not in Eiffel

  • Goto
  • Functions as arguments
  • Pointer arithmetic
  • Special increment syntax, e.g. x++, ++x
  • In-class feature overloading
slide-5
SLIDE 5

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

slide-6
SLIDE 6

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

slide-7
SLIDE 7

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

slide-8
SLIDE 8

Uniform Access

balance = deposits.total – withdrawals.total

deposits withdrawals balance deposits withdrawals

(A1) (A2)

slide-9
SLIDE 9

Attributes (fields/ data members)

bounding_rectangle: RECTANGLE

  • - Smallest rectangle including whole of current figure
slide-10
SLIDE 10

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

slide-11
SLIDE 11

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 …

slide-12
SLIDE 12

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

slide-13
SLIDE 13

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

slide-14
SLIDE 14

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

slide-15
SLIDE 15

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)

slide-16
SLIDE 16

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)

slide-17
SLIDE 17

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)

slide-18
SLIDE 18

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)

slide-19
SLIDE 19

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)

slide-20
SLIDE 20

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

slide-21
SLIDE 21

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.

slide-22
SLIDE 22

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 ...

slide-23
SLIDE 23

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)

slide-24
SLIDE 24

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.

slide-25
SLIDE 25

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 ...

slide-26
SLIDE 26

Associated convention

The notation

create x

is understood (if permitted) as an abbreviation for

create x.default_create

slide-27
SLIDE 27

To prohibit instantiating a class

class NOT_CREATABLE create

  • - Nothing here!

feature

... The rest as before ...

end

slide-28
SLIDE 28

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

slide-29
SLIDE 29

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)

slide-30
SLIDE 30

Expanded classes

You may declare a class as expanded class C ... The rest as usual ...

slide-31
SLIDE 31

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

slide-32
SLIDE 32

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)

slide-33
SLIDE 33

Basic types as expanded classes

expanded class INTEGER ... expanded class BOOLEAN ... expanded class CHARACTER ... expanded class REAL ... expanded class DOUBLE ... n: INTEGER

slide-34
SLIDE 34

Semantics of expanded types (ECMA)

An object has either reference or copy semantics

slide-35
SLIDE 35

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).

slide-36
SLIDE 36

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).

slide-37
SLIDE 37

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

slide-38
SLIDE 38

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

slide-39
SLIDE 39

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.

slide-40
SLIDE 40

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.

slide-41
SLIDE 41

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.