1 / 72

The E I F F E L Programming Language

The E I F F E L Programming Language. Claus Brabrand ((( brabrand@itu.dk ))) Associate Professor, Ph.D. ((( Software & Systems ))) IT University of Copenhagen. [ Acknowledgement : Some of the slides are inspired by a presentation on Eiffel by Martin Nordio , ETHZ ]. Eiffel.

candra
Download Presentation

The E I F F E L Programming Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. TheEIFFELProgramming Language Claus Brabrand (((brabrand@itu.dk))) Associate Professor, Ph.D. ((( Software & Systems ))) IT University of Copenhagen [ Acknowledgement: Some of the slides are inspired by a presentation on Eiffel by Martin Nordio, ETHZ ]

  2. Eiffel • Statically typed “pure” OOprogramming language (like Java and C#) • Inherent support for "Design by Contract" (DbC): • pre-conditions, post-conditions, invariants • Promotes a particular way of thinking about the design, architecture, and implementation of Object-Oriented Software Systems !

  3. Recap: Virtualization class A { void m() { print("A"); }} class B extends A { void m() { print("B"); }} class C extends A { void m() { print("C"); }} Cc = new C(); c.m(); // what is printed here? • Answers: • A) "A" • B) "B" • C) "C" • D) *** compile-time error! *** • E) *** runtime error! *** • F) I don't know ?!?

  4. Recap: Virtualization class A { void m() { print("A"); }} class B extends A { void m() { print("B"); }} class C extends A { void m() { print("C"); }} Aa = new B(); a.m(); // what is printed here? • Answers: • A) "A" • B) "B" • C) "C" • D) *** compile-time error! *** • E) *** runtime error! *** • F) I don't know ?!?

  5. Recap: Virtualization class A { void m() { print("A"); }} class B extends A { void m() { print("B"); }} class C extends A { void m() { print("C"); }} Bb = new A(); b.m(); // what is printed here? • Answers: • A) "A" • B) "B" • C) "C" • D) *** compile-time error! *** • E) *** runtime error! *** • F) I don't know ?!?

  6. Recap: Object Orientation OO benefits? • 1)Real World Apprehension (modeling) • 2)Stability of Design • 3)Code Reuse • +) It's what everyone else is using Scandinavia America Common Motivation

  7. Modeling Reality Computing = • Transformation of Representation of Information: "Informations.representations.transformation" Real world Model world abstraction R M execution: M  M' ~ M' R' concretization Abstract Concrete

  8. Recap: Inheritance DEF: class Vehicle { void move() { ... }} class Train extends Vehicle { void move() { ... }} class Plane extends Vehicle { void move() { ... }} USE: Vehicle v; ... v.move(); // shared: same method for a train and a plane! :-)

  9. Recap: Inheritance DEF: class Vehicle { void move() { ... }} class Train extends Vehicle { void move() { ... }} class Car extends Vehicle { void move() { ... }} class Plane extends Vehicle { void move() { ... }} USE: Vehicle v; ... v.move(); // unchanged (works for cars without modification)!

  10. Recap: Pre-OO Alternative DEF: datatype VEHICLE = union {// union type! train: inttrain_id; .. // vars for train | plane: intplane_id; .. // vars for plane } USE: void reserve(VEHICLE v) { if (v is train) { ... v.train_id ... } elseif (v is plane) { ... v.plane_id ... } else { error("runtime error: uninitialized vehicle!"); } }

  11. Recap: Pre-OO Alternative DEF: datatype VEHICLE = union {// union type! train: inttrain_id; .. // vars for train | plane: intplane_id; .. // vars for plane | car: intcar_id; .. // vars for car } USE: void reserve(VEHICLE v) { if (v is train) { ... v.train_id ... } elseif (v is plane) { ... v.plane_id ... } elseif (v is car) { ... v.car_id ... } else { error("runtime error: uninitialized vehicle!"); } }

  12. EIFFEL

  13. Class Declaration • Java: • Eiffel: class MyAccount { ... } class MY_ACCOUNT ... end • Convention: capitalization (possibly with underscores) for class names Convention: so-called CamelCase for class names • Syntax: "begin .. end" style • instead of curly braces "{ .. }"

  14. Constructors • Java: • Eiffel: class Account { Account() { ... } Account(intb) { ... } } class ACCOUNT create make make_balance feature make do ... end make_balance(i:INTEGER) do ... end end a routine (method) named "make" another routine, "make_balance" • Constructors have names; use "create" clause to declare routines(methods) as constructors

  15. Constructors • Java: • Eiffel: class Account { Account() { ... } Account(intb) { ... } Account(Strings) { ... } } class ACCOUNT create make make_balance make_name feature make do ... end make_balance(i:INTEGER) do ... end make_name(s: STRING) do ... end end • Constructors are given logical names; i.e., "string" vs "make_name"

  16. Basic Types • Java: • Eiffel: • boolean • char, byte • short, int • long • float • double • String <-> Boolean <-> Character <-> Integer <-> Long <-> Float <-> Double • BOOLEAN • CHARACTER • INTEGER • INTEGER_64 • REAL • DOUBLE • STRING • Just normal classes! • (no boxing/unboxing issues) • NB:Boxing / Unboxing: • "primitive types" <-> • "object wrapper classes" • Boxing:int -> Integer inti; ... Integer integer = new Integer(i); • Unboxing:int <- Integer Integer integer; ... inti = integer.intValue();

  17. No Overloading • Java: • Eiffel: class Printer { void print(booleanb) { ... } void print(inti) { ... } void print(Strings) { ... } } class PRINTER feature print_boolean(b: BOOLEAN) do ... end print_int(i:INTEGER) do ... end print_string(s: STRING) do ... end end • No overloading in Eiffel; • use type suffixes to distinguish routines

  18. Restriction not Necessarily Bad Example(1/2): • Gotos(e.g., in Basic): • Syntax: • Semantics:jump (go) to program line number 120 • Structural reasoning breaks down! • Cannot reason structurally • In particular, you'll never know if someone is or will be"jumping into the middle" of your code goto 120; "Go-to Statement Considered Harmful" by Edsgar W. Dijkstra (Communications of the ACM, 1968, 11(3): 147–148)

  19. Restriction not Necessarily Bad Example(2/2): • Pointer Arithmetic (e.g., in C): • Syntax: // Assuming p pointer • Semantics:go to the cell p is pointing to; forward 7 Person structures (i.e., sizeof(Person) number of cells); then dereference the content of that cell. Like array: • In fact: is translated into after parsing, but before semantic analysis! • Consequently: • I.e., you might just as well write array lookup as: *(p+7) Person *p; p[7] *(p+7) ≅ ⇒ ⇒ p[7] *(p+7) *(7+p) 7[p] commutativityof addition '+' 7[p]

  20. No Overloading • Java: • Eiffel: class Printer { void print(booleanb) { ... } void print(inti) { ... } void print(Strings) { ... } } class PRINTER feature print_boolean(b: BOOLEAN) do ... end print_int(i:INTEGER) do ... end print_string(s: STRING) do ... end end • No overloading in Eiffel; • use type suffixes to distinguish routines

  21. Object Creation • Java: • Eiffel: class Bank { void payBill() { Accounta; a = newAccount(); } } class BANK feature pay_bill local a: ACCOUNT do createa.make end end • local variable • declaration • Use "create" forobject creation... • ...and name the constructor explicitly • (aka, creation call)

  22. Multiple Object Creation • Java: • Eiffel: class Bank { void payBill() { Accounta1, a2; a1 = newAccount(); a2 = newAccount(100); } } class BANK feature pay_bill local a1, a2: ACCOUNT do createa1.make ; createa2.make_balance(100) end end • Semi-colon ';' optional • (for statement separation)

  23. Default Object Creation • Java: • Eiffel: • Default creation routine: • "default_create" • (inherited from "ANY") class Main { Main(inti) { super(); ... } } createa = createa.default_create • Methodsinherited from "ANY": • same_type(other) • equal(other) • deep_equal(other) • clone(other) • deep_clone(other) • print(x) • argument(n) • system(s) • ... • Implicit invocation of "super" as first statement of all constructors (for object creation and initialization, by invoking the constructor of the superclass: "Object") • ...inheriting the methods: • clone() • equals(Object) • finalize() • getClass() • hashCode() • toString() • notify() / ... • wait() / ...

  24. Default Object Creation • Java: • Eiffel: • Default creation routine: • "default_create" • (inherited from "ANY") class Main { Main(inti) { super(); ... } } createa = createa.default_create • Implicit invocation of "super" as first statement of all constructors (for object creation and initialization, by invoking the constructor of the superclass: "Object") class BANK inherit ANY redefine default_create end feature default_create do ... end end • "default_create" • is redefinable ! • ...inheriting the methods: • clone() • equals(Object) • finalize() • getClass() • hashCode() • toString() • notify() / ... • wait() / ...

  25. Groping by Feature(s) • Comment syntax: • '--' (rest of line) • Java: • Eiffel: class Account { // -- attribute (field) ------- int balance; // -- constructors ------------ Account() { .. } Account(int b) { .. } Account(String s) { .. } // -- methods ----------------- void deposit(inti) { .. } void withdraw(inti) { .. } void transfer(Account a) { .. } } class ACCOUNT feature -- Access balance:INTEGER// attribute create make make_balance make_name feature--Initialization make do .. end make_balance do .. end make_name do .. end feature-- Basic operations deposit(i: INTEGER) do .. end withdraw(i: INTEGER) do .. end transfer(a: ACCOUNT) do .. end end • The "feature" clause is used for groupingroutines and for information hiding [later].

  26. Getter vs Functional Attributes • Java: • Eiffel: DEF DEF class ACCOUNT feature -- attribute balance:INTEGER end class Account { // -- attribute --------------- private intbalance; // -- getter ------------------ intgetBalance() { // other code return balance; } // -- setter ------------------ void setBalance(intbalance) { // other code this.balance = balance; } } DEF class ACCOUNT feature -- function balance:INTEGERdo .. end end • "balance" is now afunctionreturning an INTEGER • (no longer an attribute) • Convention: • getName~ name • setName~name USE x := a.balance + 7;

  27. Terminology PROCEDURE Noresult command routine Noresult Returnsresult Computation feature (USE) FUNCTION feature (DEF) Returnsresult Computation Memory "Uniform access principle" query Memory ATTRIBUTE

  28. Expressions && Conditionals • Java: • Eiffel: • Strict conjunction • Strict conjunction void foo() { if (x & (y | z)) { x = 42; ... } } feature foo do if x and (c or d) then x := 42 ... end end • Strict disjunction • Strict disjunction • Lazy conjunction • Lazy conjunction void foo() { if (x && (y || z)) { x = 42; ... } } feature foo do if x and then (c orelsed) then x := 42 ... end end • Lazy disjunction • Lazy disjunction

  29. Loops • Java: • 'for' ⇒ 'while': • Eiffel: void foo() { for (inti=0; i<10; i++) { print(i); } } feature foo local i:INTEGER do from i:=0untili>=10 loop print(i); i := i+1; end end • NB: negated condition • "from-until-loop" { Exp1; while ( Exp2 ) { { Stm Exp3; } } } for (Exp1; Exp2; Exp3) { Stm } ⇒

  30. Inspect • Java: • Eiffel: class SwitchExample { String prettyPrint(int x) { switch(x) { case 1: return"one"; case2: return"two"; case3: return"three"; default: return"another"; } } } class INSPECT_EXAMPLE feature pretty_print(x: INTEGER):STRING do inspectx when1 thenResult := "one" when2 thenResult := "two" when 3 thenResult := "three" elseResult := "another" end end end • A routine produces a result by assigning to a special variable "Result".

  31. Uncontrolled Control Structures • Java: • Eiffel: void foo() { for (inti=0; i<10; i++) { print(x); .. .. .. .. } print(y); .. } feature foo local i:INTEGER do from i:=0 untili>=10 loop print(x); .. .. end print(y); i := i+1; end continue; break; return; -- structured control flow! • No "non-structural" control structures • (i.e., no "continue", no "break", no "return")!

  32. Infix Notation • Java: • Eiffel: • binary infix • notation! DEF class MY_CLASS feature plus (other:MY_CLASS): MY_CLASS do ... end • Not supported! alias "+" USE -- a, b, c:MY_CLASS c = a.plus(b) c = a + b-- c = a.plus(b)

  33. Infix Notation • Java: • Eiffel: • binary infix • notation! DEF class MY_POINT feature distance (other:MY_POINT): INTEGER do ... end • Not supported! alias "|-|" USE -- p, q :MY_POINT if p.distance(q) > 10 then .. end if (p |-| q) > 10 then .. end • NB:be aware of • precedence and • associativityrules

  34. Infix Notation • Java: • Eiffel: • unary prefix • notation! DEF class MY_CLASS feature uminus:MY_CLASS do ... end • Not supported! alias "-" USE -- x, y :MY_CLASS y = x.uminus() y = -x -- y = x.uminus

  35. Equivalence Relation '=' (S, '=') equivalence relation'=' ⊆ S×S: • Reflexive: • ∀x∈S: x = x • Symmetric: • ∀x,y∈S: x = y ⇔ y = x • Transitive: • ∀x,y,z∈S: x = y ⋀ y = z ⇒ x = z

  36. Partial Order Relation '≤' (S, '≤') partial order relation'≤' ⊆ S×S: • Reflexive: • ∀x∈S: x ≤ x • Anti-Symmetric: • ∀x,y∈S: x ≤ y ⋀x ≤ y ⇒ x = y • Transitive: • ∀x,y,z∈S: x ≤ y ⋀ y ≤ z ⇒ x ≤ z

  37. Inheritance

  38. Inheritance • Java: • Eiffel: class Account extendsObject { ... } class ACCOUNT inheritANY ... end ANY Object LIST ACCOUNT ... VEHICLE List<?> Account ... Vehicle TRAIN Train PLANE Plane NONE • Multiple Inheritancewith"NONE" as the subclass of all classes! • [more about this later]

  39. Redefinition of Routines • Java: • Eiffel: class Account extendsObject { StringtoString() { // overridereturn "abc"; } } class ACCOUNT inheritANY redefine out end feature out:STRING-- redefinition Result := "abc" end end • Method "out" is implicitly overwritten • Routine: "out" is explicitly redefined

  40. Precursor (super) • Java: • Eiffel: class Account extendsObject { String toString() { // override return super(); } } class ACCOUNT inheritANY redefine out end feature out:STRING-- redefinition Result := Precursor end end {ANY} • "super()" will cause invocation of the method with the same name (here, "toString()") in the super class(here, "Object") • "Precursor()" will cause invocation of the routine with the same name (here "out()") in the heir class(here, "ANY") • In case of multiple inheritance, • the heir has to be specified "{ANY}" • (optional in case of a single heir)

  41. Multiple Inheritance • Java: • Eiffel: class A feature foodo print "A"end end • Java does not support multiple inheritance! class B feature foodo print "B"end end class C inherit A B end end undefinefoo end • Name clashes can be avoided by undefining features(routines and attributes)

  42. Multiple Inheritance • Java: • Eiffel: class A feature foodo print "A"end end • Java does not support multiple inheritance! class B feature foodo print "B"end end class C inherit A B end end renamefoo asfoo_b end • Name clashes can be avoided by renamingfeatures(routines and attributes)

  43. Frozen (final) Classes • Java: • Eiffel: final class Account { ... } frozen classACCOUNT ... end • A frozen class can not be inherited • A final class can not be extended

  44. Frozen Routines (final methods) • Java: • Eiffel: class Account { final void deposit(inti) { ... } } class ACCOUNT feature frozendeposit(i:INTEGER) do ... end end • A final method can not be overwritten • A frozen routine can not be redefined • Also, all of its args are frozen

  45. Frozen Routines (final methods) • Java: • Eiffel: • May not be subclassed! DEF class A { void m() { System.out.println("test"); } } class F{ final void fin(A a) { a.m(); } } class B extends A { void m() { // override System.out.println("he he!"); } } class ACCOUNT feature frozendeposit(i:INTEGER) do ... end end • A frozen routine can not be redefined • Also, all of its args are frozen USE // A a; F f; ... f.fin(a); a= new B(); • Output: • "he he!"

  46. Deferred (abstract) Classes • Java: • Eiffel: abstract class Vehicle { abstract void move(inti) { ... } } deferred classVEHICLE feature deferredmove(i:INTEGER) end end • NB: A class with at least one abstract method has to be abstract ! • NB: A class with at least one deferred routine has to be deferred !

  47. Parameter Mechanisms • Java: • Eiffel: DEF DEF class Main { void m(Account x,int y) { x.deposit(1); y = y+1; } } class MAIN feature m(x:ACCOUNT; y:INTEGER) do x.deposit(1) -- y := y+1 end end • Not allowed to change • value of a parameter ! USE USE // Account a; int b; a = new Account(2); b = 2; m(a, b); System.out.println(a); System.out.println(b); -- a:ACCOUNT; b:INTEGER a.make_balance(2) b := 2 m(a, b) a.print b.print • Output:3 • 2 • Output:3 • 2 • Class typeshave "call by reference" semantics; i.e., they are shared! • Class typeshave "call by reference" semantics; i.e., they are shared! • Primitive typeshave "call by value" semantics; i.e., they are copied! • Expanded typeshave "call by value" semantics; i.e., they are copied!

  48. Expanded Types • Java: • Eiffel: DEF expandedclass ACCOUNT ... end USE -- a:ACCOUNT a.print x.f(a) -- a copied! -- not passed by ref! a.print • The two print statements will always print the exact same • (Routine 'f' of object 'x' will side-effect a copyof a)

  49. Exception Handling • Java: • Eiffel: class Printer { void print(inti) { try { ... ... } catch(Exception e) { // handle exception } } } class PRINTER feature print_int(i:INTEGER) do ... ... rescue -- handle exception end end • "throw" for raising exceptions throw new Exception(); • (create {MY_EXCEPTION}) • .raise • Control structure: • "try-catch" for exception handling • (via exceptional • control flow)

  50. Once Routines • Simulated in Java: • Eiffel: static int fib(int n) { if (n==0) return 0; else if (n==1) return 1; else return fib(n-2) + fib(n-1); } feature fib(n:INTEGER):INTEGER do if n=0 then Result := 0 elseif n=1 then Result := 1 else Result := fib(n-2) + fib(n-1) end end static Integer cache = null; static intfoo() { if (cache==null) { cache = fib(30); } return cache;} • "once" routines cache their results • (built in) feature foo:INTEGER once Result := fib(30) end • Can be simulated using dynamic programming • (aka, "memoization") • Relies on (static) global variables • (singleton pattern) ! print(foo()) // calculated print(foo()) // cached :-) print(foo) -- calculated print(foo) -- cached :-) • 832040 [slow] • 832040 [fast] • 832040 [slow] • 832040 [fast]

More Related