Files
eiffel-org/documentation/current/method/eiffel-tutorial-et/et-hello-world.wiki
halw 75fd305e5d Author:halw
Date:2008-09-17T14:29:45.000000Z


git-svn-id: https://svn.eiffel.com/eiffel-org/trunk@4 abb3cda0-5349-4a8f-a601-0c33ac3a8c38
2008-09-17 14:29:45 +00:00

79 lines
4.4 KiB
Plaintext

[[Property:title|4 Hello World]]
[[Property:link_title|ET: Hello World]]
[[Property:weight|-12]]
When discovering any approach to software construction, however ambitious its goals, it is reassuring to see first a small example of the big picture -- a complete program to print the famous "Hello World" string. Here is how to perform this fascinating task in the Eiffel notation.
You write a class <code> HELLO </code> with a single procedure, say <code> make </code>, also serving as creation procedure. If you like short texts, here is a minimal version:
<code>
class
HELLO
create
make
feature
make is
do
print ("Hello World%N")
end
end
</code>
In practice, however, the Eiffel style rules suggest a better documented version:
<code>
indexing
description: "Root for trivial system printing a message"
author: "Elizabeth W. Brown"
class
HELLO
create
make
feature
make is
-- Print a simple message.
do
io.put_string ("Hello World")
io.put_new_line
end
end -- class HELLO
</code>
The two versions perform identically; the following comments will cover the more complete second one.
Note the absence of semicolons and other syntactic clatter or clutter. You may in fact use semicolons to separate instructions and declarations. But the language's syntax is designed to make the semicolon optional (regardless of text layout) and it's best for readability to omit it, except in the special case of successive elements on a single line.
The <code> indexing </code> clause does not affect execution semantics; you may use it to associate documentation with the class, so that browsers and other indexing and retrieval tools can help users in search of reusable components satisfying certain properties. Here we see two indexing entries, labeled <code> description </code> and <code> author </code>.
The name of the class is <code> HELLO </code>. Any class may contain "features"; <code> HELLO </code>has just one, called <code> make </code>. The <code> create </code> clause indicates that <code> make </code> is a "creation procedure", that is to say an operation to be executed at class instantiation time. The class could have any number of creation procedures.
The definition of <code> make </code> appears in a <code> feature </code> clause. There may be any number of such clauses (to separate features into logical categories), and each may contain any number of feature declarations. Here we have only one.
The line starting with <code> -- </code> (two hyphen signs) is a comment; more precisely it is a "header comment", which style rules invite software developers to write for every such feature, just after the <code> is </code>. As will be seen in [[8 Design by Contract (tm), Assertions and Exceptions|"The contract form of a class", page 44]] , the tools of EiffelStudio know about this convention and use it to include the header comment in the automatically generated class documentation.
The body of the feature is introduced by the <code> do </code> keyword and terminated by <code> end </code>. It consists of two output instructions. They both use <code> io </code>, a generally available reference to an object that provides access to standard input and output mechanisms; the notation <code> io </code> . <code> f </code>, for some feature <code> f </code> of the corresponding library class ( <code> STD_FILES </code>), means "apply <code> f </code> to <code> io </code>". Here we use two such features:
* <code> put_string </code> outputs a string, passed as argument, here <code> "Hello World" </code>.
* <code> put_new_line </code> terminates the line.
Rather than using a call to <code> put_new_line </code>, the first version of the class simply includes a new-line character, denoted as <code> %N </code>, at the end of the string. Either technique is acceptable.
To build the system and execute it:
* Start EiffelStudio
* Create a new ''Basic application'' project
* Specify <code> HELLO </code> as the "root class" and <code> make </code> as the "root procedure".
* You can either use EiffelStudio to type in the above class text, or you may use any text editor and store the result into a file <code> hello.e </code> in the current directory.
* Click the "Compile" icon.
* Click the "Run" icon.
Execution starts and outputs <code> Hello World </code> on the appropriate medium: under Windows, a Console; under Unix or OpenVMS, the windows from which you started EiffelStudio.