Computer Science Canada Work In Progress - Eiffel Whirlwind |
| Author: | wtd [ Sat Jul 14, 2007 3:53 pm ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Post subject: | Work In Progress - Eiffel Whirlwind | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Theory Warning! Eiffel is a purely object-oriented, statically-typed, manifestly-typed programming language. As it is purely object-oriented, everything is an object. Everything exists within a class. All things within classes are "features" and are either variables, constants, or routines. Routines are either functions, which return a new object and make no changes to the class, or procedures which return nothing, and do have side-effects on the class. And that's enough theory for now. The Simplest Possible Class
Class names are always all-caps, and have words separated by underscores. Let's Add a Feature
Here our feature is a constant string. Because it's a constant, it's capitalized. Let's Add Another
Let's Add a Routine
A Variable
Initializing That Variable We need a creation routine. They're just like regular routines. Except that in their case they also get a mention in the create clause. The "make" routine is the most commonly used name.
This is Just a Class We have yet to create a program. A program needs an entry point. And entry point in an Eiffel program is just a creation routine. By default the compiler will choose the "make" routine, but we can tell it to use any creation routine as the entry point. That will be useful, since "make" in the above does very little in terms of output.
There's a problem, though. When entry_point is executed, nothing has actually been assigned to "name" meaning that it remains Void.
Now, we can compile with:
And run it with:
Which results in:
The "-clean" option removes all of the intermediate files the SmartEiffel compiler creates as it compiles the code to C, and then uses a C compiler to generate the final executable. Another Class
Using It
Compile with:
Inhertitance and Some Polymorphism
Deferred Features
And now to rewrite our previous code to take advantage of this.
Building Made Easier Instead of typing out the SmartEiffel compiler command each time, let's use an ACE file named "hello_world.ace".
First we define the name of the system being created. Then we specify the root class and the creation routine to use as the entry point. Then we set a series of defaults. Here the only one I've bothered with is turning garbage collection on. Next we define clusters. Clusters allow us to group classes, and tell the compiler where to look for classes. The "standard" cluster simply points to the standard library, with the help of an environment variable. The hello_world_cluster points to the current directory. Within the generate clause I can set a series of options for the generated C code. I tell it to clean up after the build. I can run this with:
Making Choices
Iterating
Streamlining with Agents
Polymorphism Bites Back So with NAME and FORMAL_NAME we saw some polymorphism. Even if we specified that a variable was a NAME, and assigned a FORMAL_NAME to it (via creation or otherwise), the FORMAL_NAME version of full_name would be used. This is really really good. But... What about the title feature? NAME has no such thing. As such, anything we have guaranteed only to be a name cannot be guaranteed to have a title feature.
The ?= operator serves the same purpose as :=, but evaluates whether or not it will succeed at run-time, rather than compile-time. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Author: | Clayton [ Sat Jul 14, 2007 7:31 pm ] |
| Post subject: | RE:Work In Progress - Eiffel Whirlwind |
Awesome stuff wtd! Definitely something I have to check out. Quick query, does indentation have anything to do with how code is seperated (like Python par example)? |
|
| Author: | wtd [ Sat Jul 14, 2007 7:33 pm ] |
| Post subject: | RE:Work In Progress - Eiffel Whirlwind |
Nope. Just makes it look nice. |
|
| Author: | Clayton [ Sat Jul 14, 2007 7:34 pm ] |
| Post subject: | RE:Work In Progress - Eiffel Whirlwind |
so under the create clause nothing to do with indentation then? Just as long as you have a comma in between each word I'm guessing? |
|
| Author: | wtd [ Sat Jul 14, 2007 11:51 pm ] |
| Post subject: | RE:Work In Progress - Eiffel Whirlwind |
Yes. |
|
| Author: | rdrake [ Tue Jul 17, 2007 2:34 am ] | ||||
| Post subject: | Re: Work In Progress - Eiffel Whirlwind | ||||
For the benefit of those interested, the following sample was created by myself with (massive) help from wtd. It shows some overriding of methods, hiding of methods, and probably some other stuff too. Enjoy!
Naturally you compile it with this:
|
|||||
| Author: | wtd [ Tue Jul 17, 2007 2:37 am ] |
| Post subject: | RE:Work In Progress - Eiffel Whirlwind |
Excellent! |
|
| Author: | Aziz [ Tue Jul 17, 2007 8:24 am ] |
| Post subject: | RE:Work In Progress - Eiffel Whirlwind |
It seems very verbose to me, though interesting. What did you mean by manifestly-typed, wtd? |
|
| Author: | wtd [ Tue Jul 17, 2007 1:54 pm ] |
| Post subject: | RE:Work In Progress - Eiffel Whirlwind |
Types have to be explicitly declared, as in Java, C++, etc. This is as opposed to something like O'Caml. As for verbosity, Bertrand Meyer's perspective, I believe, was that one would have verbose design documents to explain away terse code, so he felt it better to just have code that was as self-documenting as possible. |
|
| Author: | Aziz [ Tue Jul 17, 2007 2:10 pm ] |
| Post subject: | RE:Work In Progress - Eiffel Whirlwind |
Is inherit ANY the likes of deriving from a base object class? Also, could you expand on the 'create' routines, etc? EDIT: What would you do if you had a long line? I take it line breaks are the statement separators. |
|
| Author: | wtd [ Tue Jul 17, 2007 5:34 pm ] | ||||
| Post subject: | RE:Work In Progress - Eiffel Whirlwind | ||||
Yes, ANY is the root of the inheritance hierarchy. You automatically inherit from ANY, but sometimes it needs to be explicit so that you can redefine, rename or undefine features from ANY. Create routines are routines which may be used when an object is created in order to give it an initial state. Create routines may be used elsewhere in the program as well. You need do nothing to continue long lines.
|
|||||
| Author: | Aziz [ Tue Jul 17, 2007 5:38 pm ] |
| Post subject: | RE:Work In Progress - Eiffel Whirlwind |
What about polymorphism? Do virtual methods need to be defined explicitly, such as in C#, or or they more like Java? And how would this be handled when you undefine a feature? |
|
| Author: | wtd [ Tue Jul 17, 2007 5:42 pm ] | ||||||
| Post subject: | RE:Work In Progress - Eiffel Whirlwind | ||||||
|
|||||||
| Author: | Aziz [ Tue Jul 17, 2007 6:05 pm ] |
| Post subject: | RE:Work In Progress - Eiffel Whirlwind |
Running test would output "Bar". |
|