language basics timeline
play

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-


  1. - 3 - Language basics

  2. 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- international.org/publications/standards/ Ecma-367.htm See roadmap: http://eiffelsoftware.origo.ethz.ch/index.php/Language_road_map

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

  4. What is not in Eiffel Goto � Functions as arguments � Pointer arithmetic � Special increment syntax, e.g. x++, ++x � In-class feature overloading �

  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

  6. Kinds of feature Client view Internal view (specification) (implementation) Command Procedure Routine No result Computation Feature Feature Feature Feature Memory Returns result Function Computation Query Memory Attribute

  7. Uniform access principle It doesn‘t matter to the client whether you look up or compute A call such as my_account . balance could use an attribute or a function

  8. Uniform Access deposits (A1) withdrawals deposits (A2) withdrawals balance balance = deposits . total – withdrawals . total

  9. Attributes (fields/ data members) bounding_rectangle: RECTANGLE -- Smallest rectangle including whole of current figure

  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

  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 … As before … Result . lower_left = lower_left Result . contains ( Current ) end

  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

  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

  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

  15. Use of the class in a client (1/ 5) class GRAPHICS feature p , q : POINT p 0.0 -- Graphic points … 0.0 some_routine is -- Use p and q . (POINT) local u , v : REAL do -- Creation instructions q 0.0 create p create q 0.0 (POINT) end end

  16. Use of the class in a client (2/ 5) class GRAPHICS feature p , q : POINT p 4.0 -- Graphic points … -2.0 some_routine is -- Use p and q . (POINT) local u , v : REAL do -- Creation instructions q 0.0 create p create q 0.0 p . move (4.0, -2.0) (POINT) -- Compare with Pascal, C, Ada: -- Move (p, 4.0, -2.0) end end

  17. Use of the class in a client (3/ 5) class GRAPHICS feature p , q : POINT p 2.0 -- Graphic points … -1.0 some_routine is -- Use p and q . (POINT) local u , v : REAL do -- Creation instructions q 0.0 create p create q 0.0 p . move (4.0, -2.0) (POINT) -- Compare with Pascal, C, Ada: -- Move (p, 4.0, -2.0) p . scale (0.5) end end

  18. Use of the class in a client (4/ 5) class GRAPHICS feature p , q : POINT p 2.0 -- Graphic points … -1.0 some_routine is -- Use p and q . (POINT) local u , v : REAL do -- Creation instructions q 0.0 create p create q 0.0 p . move (4.0, -2.0) (POINT) -- 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

  19. Use of the class in a client (5/ 5) class GRAPHICS feature p , q : POINT p 2.0 -- Graphic points … -1.0 some_routine is -- Use p and q . (POINT) local u , v : REAL do -- Creation instructions q 0.0 create p create q 0.0 p . move (4.0, -2.0) (POINT) -- 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

  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

  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 .

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

  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)

  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.

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

  26. Associated convention The notation create x is understood (if permitted) as an abbreviation for create x . default_create

  27. To prohibit instantiating a class class NOT_CREATABLE create -- Nothing here! feature ... The rest as before ... end

  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

  29. Types Reference types; value of an entity is a reference Example: b : SECURITY b ( SECURITY ) Expanded types; value of an entity is an object. Example: d : expanded SECURITY d ( SECURITY )

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

  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

  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)

  33. Basic types as expanded classes expanded class INTEGER ... expanded class BOOLEAN ... expanded class CHARACTER ... expanded class REAL ... expanded class DOUBLE ... n : INTEGER

  34. Semantics of expanded types (ECMA) An object has either reference or copy semantics

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

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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend