Hello World in Eiffel
Your first Eiffel program - the classic Hello World example with Docker setup using EiffelStudio
Every programming journey starts with Hello World. Let’s write our first Eiffel program using EiffelStudio, the primary Eiffel development environment.
The Code
Create a file named hello.e:
class
HELLO
create
make
feature
make
do
print ("Hello, World!%N")
end
end
Understanding the Code
Eiffel is a pure object-oriented language - everything is a class. Let’s break down each part:
class HELLO- Declares a class named HELLO (class names are uppercase by convention)create make- Specifies thatmakeis a creation procedure (constructor)feature- Introduces a section of class features (methods and attributes)make- The entry point procedure called when the program runsdo ... end- Marks the executable body of the procedureprint ("Hello, World!%N")- Outputs text to the console%N- Eiffel’s newline character (similar to\nin C-like languages)
The Class Structure
Every Eiffel program is built from classes. The minimal structure is:
class
CLASS_NAME
create
creation_procedure_name
feature
creation_procedure_name
do
-- code here
end
end
Eiffel enforces that every object is properly initialized through creation procedures (constructors).
The Feature Section
feature introduces class members. You can have multiple feature clauses with different export statuses:
feature -- Available to all clients
feature {NONE} -- Private to this class
feature {FRIEND} -- Available only to FRIEND class
The Print Procedure
print is inherited from class ANY, which all Eiffel classes implicitly inherit from. It accepts any object and converts it to a string representation for output.
Running with Docker
Eiffel requires a configuration file (.ecf) and compilation before running. Here’s how to do it with Docker:
| |
Understanding the Docker Command
ec- The Eiffel compiler command-batch- Run without user interaction-config hello.ecf- Use the specified configuration file-c_compile- Automatically compile the generated C code./EIFGENs/hello/W_code/hello- Run the compiled executable
The Eiffel compiler:
- Parses and analyzes your Eiffel code
- Generates optimized C code
- Compiles the C code to a native executable
- Places the result in
EIFGENs/<system>/W_code/
The ECF Configuration File
Eiffel projects require an ECF (Eiffel Configuration File) that specifies:
- system name - The project name
- target - Build configuration settings
- root class and feature - The entry point
- libraries - Which Eiffel libraries to use
- clusters - Where to find your source files
The base library provides fundamental classes like STRING, INTEGER, and the print procedure through class ANY.
Expected Output
Hello, World!
Alternative Approaches
Eiffel offers several ways to output text:
Using io.put_string
The more “proper” Eiffel style uses io:
class
HELLO
create
make
feature
make
do
io.put_string ("Hello, World!")
io.put_new_line
end
end
io- An inherited feature fromANYproviding console I/Oput_string- Outputs a string without newlineput_new_line- Outputs a newline
With Documentation (Recommended Style)
Eiffel style guides recommend documenting classes and features:
note
description: "Classic Hello World program"
author: "Your Name"
date: "$Date$"
class
HELLO
create
make
feature -- Initialization
make
-- Print greeting message.
do
print ("Hello, World!%N")
end
end
note- Metadata section for documentation-- Comment- Comments start with double dashfeature -- Initialization- Feature clause with a comment header
Eiffel Naming Conventions
| Element | Convention | Example |
|---|---|---|
| Class names | ALL_CAPS | HELLO, BANK_ACCOUNT |
| Feature names | lowercase | make, deposit |
| Local variables | lowercase | i, count |
| Constants | lowercase | max_size |
| File names | class_name.e | hello.e, bank_account.e |
Note: One class per file, filename matches class name (lowercase).
The Eiffel Compilation Process
Eiffel uses a unique compilation approach:
hello.e → ec compiler → C code → C compiler → executable
Compilation Modes
- Workbench (W_code) - Fast compilation for development
- Finalized (F_code) - Optimized for deployment
For Hello World, workbench mode is sufficient. For production, you’d use:
| |
Why Generate C?
Eiffel compiles to C for several reasons:
- Portability across platforms
- Leverages mature C compiler optimizations
- Easy integration with C libraries
- No runtime dependency (unlike JVM languages)
Common Beginner Mistakes
Missing Create Clause
class HELLO
feature
make
do
print ("Hello, World!%N")
end
end
-- Error: no creation procedure specified
Every class that will be instantiated needs a create clause.
Wrong String Escape
print ("Hello\nWorld") -- ❌ Wrong: C-style escape
print ("Hello%NWorld") -- ✅ Correct: Eiffel-style escape
Eiffel uses % for special characters:
%N- Newline%T- Tab%%- Percent sign%"- Double quote
Class Name Mismatch
The class name must match the filename (case-insensitively):
hello.eshould containclass HELLObank_account.eshould containclass BANK_ACCOUNT
Installing EiffelStudio Locally
If you prefer to run Eiffel without Docker:
Ubuntu/Debian:
| |
macOS:
| |
Windows:
| |
After installation, you can use the EiffelStudio IDE or the ec command-line compiler.
A Bit of History
Eiffel was created by Bertrand Meyer in 1986 at Interactive Software Engineering (now Eiffel Software). Meyer, who had worked with Ada and formal specification methods, designed Eiffel to bring mathematical rigor to software development.
The language introduced Design by Contract - the idea that software components should have explicit contracts (preconditions, postconditions, and invariants) just like business contracts. This revolutionary concept influenced virtually every object-oriented language that followed.
The name “Eiffel” comes from the Eiffel Tower, symbolizing the language’s goal of combining engineering precision with elegant design.
Why Eiffel for Hello World?
Even in this simple example, you see Eiffel’s philosophy:
- Pure Object-Orientation - Everything is a class, including the program
- Explicit Initialization - Creation procedures ensure proper setup
- Clear Structure - Keywords like
class,create,feature,do,endmake structure visible - Configuration Separate from Code - ECF files separate build settings from source
These features become essential in large systems where Eiffel’s Design by Contract shines.
Next Steps
Continue to Design by Contract to learn about Eiffel’s most powerful feature - preconditions, postconditions, and invariants that make your code provably correct.
Key Takeaways
- Everything is a class in Eiffel - there’s no “main” function
- Creation procedures (constructors) must be explicitly declared with
create printoutputs to console,%Nis the newline character- ECF files configure the project, libraries, and build settings
- Eiffel compiles to C, producing native executables
- Class names are UPPERCASE, feature names are lowercase
- One class per file, filename matches class name
Running Today
All examples can be run using Docker:
docker pull eiffel/eiffel:latest