Tutorial tour

From Liberty Eiffel Wiki
Revision as of 21:21, 1 April 2016 by Ramack (talk | contribs) (added loop chapter)
Jump to navigation Jump to search

Welcome to the guided tour of the tutorial!

All the classes in the tutorial are provided with your LibertyEiffel installation, in the tutorial directory.

Easy starting: Hello World!

To compile the program, go to the LibertyEiffel/tutorial directory and execute the command:

se c HELLO_WORLD make -o hello

The meaning of the command is

se the front-end tool for the LibertyEiffel compiler
c the tool invoked by se for compile into an executable
HELLO_WORLD the class of which an object is created (you may also write it in lower-case, or use its filename with the .e extension) we call it root class, as this is the root of the programs object tree
make the creation procedure of the HELLO_WORLD class to call
-o hello generates an executable with the name hello (linux) or hello.exe (windows)

The command produces an executable, usually hello or hello.exe depending on the system. After compiling is finished, you can run the executable.

This unavoidable program is in the file hello_world.e and lets you grasp the basic concepts of Eiffel. Those concepts are:

  • Everything is a class. In Eiffel, outside classes there is no salvation.
  • The program starts by creating an object (of the root class) and initializing it by execution of the given root creation procedure. Here, the make feature in the class HELLO_WORLD.
  • Each file is named after the name of the class it contains, in lower-case, with the .e extension.
  • Each class comes with a set of features, all its attributes and methods which come in form of functions (if they return a result) or procedures (in case they just 'do' something)
  • Note the special object io that allows you to write text on the standard output. We will see that it also lets you read data.
  • For the Eiffel syntax, look here.

Please take a look at the following examples in the tutorial folder to see some classical example programs (some of them are in organized in folders for better overview)

  • fibonacci.e
  • knight.e
  • pyramide.e and pyramide2.e
  • gcd
  • hanoi
  • parking
  • triangle

To compile the other samples you must obviously modify the compiler command to give another root class .

Some important concepts

Comments

As in every programming language there is the possibility to write comments into the source code, which do not have any effect for the final program, but are for the developer or reader of the source code to get an understanding of the ideas and algorithms behind the code. A comment in Eiffel starts with the sequence of a couple of minus signs and is terminated by a newline (RETURN).

-- this is a comment in Eiffel
a := b + 3 -- comments are also allowed after some code

Manifest notations

LibertyEiffel comes with a big set of available datatypes. Famous examples as INTEGER, BOOLEAN, CHARACTER and STRING. For those (and also many collection types) there is the possibility to directly denote them in the program code, using the notations

123
True, False
'a'
"STRING"

See tutorial file manifest_notation.e for an example with nearly all options to define objects by manifest notations.

Everything is an Object

As mentioned above, in Eiffel every useful thing is an object. There are two different kinds of objects: expanded objects and referenced object. Expanded objects are passed by value, while reference objects are - who guesses - passed by reference. In case you write 7 in the source code this is a shorthand for an instance of the expanded type INTEGER and therefore it is an expanded object. Whenever you assign it to a variable or pass it to another feature it gets copied. A STRING object - e. g. denoted by "Hello World!" in the source code is a reference object, created implicitly by the compiler, allocated on the heap and passed by reference.

Local variables and Assignments

Every routine may define variables in a "local block" before its body (which start with the keyword do):

make
   local
      a: INTEGER
      s: STRING
   do
      a := 5
      a := a + 1
      s := "Hello World!"
   end

The visibility of these variables is limited to the method they are defined in and they do not keep their value from one execution of that routine to another. Assignment is done by the assignment operator ":=" which changes the variable (or let's say writable entity) on the left side to the value of the expression on the right side. In case of the "Hello World!" assignment a new object of type STRING is created on the heap, initialized to the text "Hello World!" and a link to this object is stored in the local variable s.

Loops

In Eiffel we have exactly one construct to do something repeatedly, so no need to learn about for, do-while, while, anyhow, one is sufficient:

from
  -- initialization
invariant
  -- what shall stay true during repetition
variant
  -- non-negative, but decreasing numeric value
until
  -- termination condition
loop
  -- what to do several times
end

Note, that the termination condition is an expression of type BOOLEAN and the loop body is executed until the condition evaluates to True, which differs in the logic from e. g. the loop constructs in C.

The most strange things for newcomers are probably the invariant and the variant. While the first is a BOOLEAN condition, that stays true for every iteration, the latter is an INTEGER value which decreases with every iteration and always stays positive and therefore shows that the loop terminates. Both are optional and are not used very often in practice, even though they are essential in case we want to prove a program to be totally correct (and therefore terminates). I fear, that these formal loop assertions stay more or less unused until a formal verification tool is able to take real benefit from them.

Input-output

  • io
  • directory
  • basic_directory?

Arguments

print_arguments.e

Feature calls

Contracts

Contracts are the main reason why Eiffel is superior to many other languages... ...

Garbage collection

Eiffel is garbage collected. That means, the objects created on the heap will automatically be freed after they lost reachability from the rest of the executing program.

ACE files

Collections

including iterator and sorting

Downcasting

downcasting.e

To go further

Agents

... and tuples

external

cecil

Memory management

memory

Extension libraries

Random

Date & time

Storable

Sequencer

Vision

visiopn, signal

Network

Execution