Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Eiffel-tut] Bonjour, Eiffel!
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Sat Jul 03, 2004 10:10 pm   Post subject: [Eiffel-tut] Bonjour, Eiffel!

This is my attempt to introduce the basics of Eiffel with the classic "Hello, World!" program, much as I did in "Hello, Ruby!".

Like Ruby, Eiffel is entirely object-oriented, though not quite as entirely. Classes in Eiffel are not objects themselves. Also, Ruby's syntax was in part inspired by Eiffel's, so if you've read my Ruby tutorials the syntax might somewhat familiar.

Eiffel is a much stricter language that Ruby. As in Java, there is a limit of one class per source file. Unlike Java, Eiffel has no "packages", so all classes exist in a single namespace. This alleviates the need for "import" statements.

Also unlike most other languages, there is no particular procedure which must construct an object. Any procedure may be designated as being able to create an object. The standard is "make".

The equivalent to "main" in Eiffel is a class whose "make" routine serves that purpose.

So, let's see some code already.

code:
-- This is a comment.
-- File: bonjour_eiffel.e
class
   BONJOUR_EIFFEL
creation
   make
feature { ANY }
   make is
      do
         std_output.put_string("Bonjour, Eiffel!")
         std_output.put_new_line
      end
end


Obviously there's a bit more overhead here than there is for the equivalent Ruby program. Wink

code:
puts "Bonjour, Eiffel!"


It all has a purpose, though. The "class", "creation", and "feature" lines all introduce what Eiffel refers to as clauses. The class clause simply lists the name of the class. Class names in Eiffel are always all capital letters.

The "creation" clause lists which procedures can be used as creation routines.

The "feature" clause takes a bit more explaining. In Eiffel "feaures" encompasses variables and routines (functions and procedures). The class name in curly brackets is the root class ANY. Since every class inherits from this, and therefore "is a" ANY. This is important because the class name or names listed in the curly brackets specify the class or classes which can access that given feature. ANY is equivalent to "public", while NONE is equivalent to "private". There is no need for "protected", since access can be controlled in much greater detail.

Moving along, it's important to note that, like Ruby, Eiffel eschews parentheses where they aren't needed. The "make" procedure takes no arguments, so there's no reason it should have parentheses. Eiffel takes this further than Ruby, though. Where parentheses are not needed, they cannot be used. Using them results in a syntax error.

The "do" clause in the "make" procedure specifies the code to actually run when the routine is called. There are a number of other clauses we could use, but which serve no purpose in this example.

The actual code being run is fairly straightforward. The "put_string" and "put_new_line" methods are being called on the "std_output" variable which all classes inherit by default.

So, assuming you have a Eiffel compiler and have figured out how to use it (the SmartEiffel compiler is my favorite for its simplicity), you can now create a native executable, run it, and be overwhelmed by "Bonjour, Eiffel!" appearing on your screen.

Taking it up a notch

I also demonstrated in "Hello, Ruby!" how to have Ruby say hello back. It's only fair I do the same here. For this I'll introduce local variables and the "std_input" variable.

code:
-- File: bonjour_eiffel.e
class
   BONJOUR_EIFFEL
creation
   make
feature { ANY }
   make is
      local
         name : STRING
      do
         std_output.put_string("Bonjour, Eiffel!")
         std_output.put_new_line
         
         std_input.read_line
         name := std_input.last_string
         
         std_output.put_string("Eiffel says, \"Bonjour, " + name + "!\"")
         std_output.put_new_line
      end
end


Firs we've added another clause to our "make" routine. The "local" clause lists variables for use solely in the "make" routine. In this case we've declared a variable named "name" of class STRING.

STRING objects do not need to be explicitly created, so we're saved that step. The first two lines of executed code look familiar. The next two don't though.

They're faairly self-explanatory. The "read_line" procedure of "std_input" reads a string from standard input. On the next line we assign that string to "name". It's been stored in the "last_string" feature of "std_input".

Why not just compact it to one line? Well, Eiffel enforces the difference between functions and procedures. Functions may return an object, but they may not alter the state of non-local variables. The "read_line" procedure alters the variables of "std_input", so it cannot return the value it's read. Instead, it alters another variable, the "last_string" feature, where it stores the string it's just read. Since merely reading that variable doesn't alter anything, we can have code like the following on the next line.

code:
name := std_input.last_string


Printing out Eiffel's response should look fairly familiar to Java programmers. The + operator of the STRING class is defined to concatenate STRING objects together.

I'd be happy to put together more Eiffel tutorials if there's interest.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: