ikhono.net

CAT calls in Eiffel 0

Posted on Monday, December 10

Eiffel is statically typed, which means that type checking is performed during compile-time as opposed to run-time. Therefore it should be guaranteed that no execution of a valid program will ever produce a run-time type failure.

To be a little bit more precise, an Eiffel program is class-level-valid if it satisfies the following properties:

  • In every assignment x := y - either explicit or through argument passing - type of y conforms to type of x.
  • In every qualified feature call x.f(...), f is an exported feature of the class of x.

Without so-called CAT calls, any class-level-valid program would be type-safe. The acronym CAT stands for Change of Availability or Type.

No cat calls

Photo credit

Continue reading...

Convertibility in Eiffel 0

Posted on Saturday, December 08

Conformance determines when a type may be used instead of another. The conformance mechanism relies on inheritance:

A is conform to B if the base class of A is a descendant of the base class of B.

Convertibility completes the conformance mechanism. Convertibility lets you perform assignment and argument passing in cases where conformance does not hold but you still want the operation to succeed after adapting the source value to the target type. For example:


my_real := 42   -- my_real: REAL, i.e. my_real is of type REAL

Many classes in Eiffel Base, like INTEGER, STRING, etc. support conversion. In this article I’d like to show you how to add convertibility to your own classes.

1/2

Photo credit

Continue reading...

With a little help from my friends 0

Posted on Friday, November 30

In this article I’d like to present you some of my favourite Rails helper methods. Just add them to your app/helpers/application.rb file to use in your own application.

Booleans

This is maybe one of the most basic helpers you’ll ever encounter. But it also helps you DRY up your code, e.g. if your application is multilingual.

1
2
3
def yes_no(bool)
  bool ? "yes" : "no"
end

Numbers

The following helper prints numbers with correct delimiters and a fitting form of title.

1
2
3
def print_number(title, number)
  number_with_delimiter(number) + " " + title.pluralize(number)
end

Page title

The title helper helps to DRY up page titles:

1
2
3
4
5
def title(page_title)
  content_for(:title) {
    page_title
  }
end

I found this helper on Railscasts.

In app/views/layouts/application.rhtml I use the following two snippets:


<title>MySite :: <%= yield(:title) || "My uber cool site!" %></title>
1
2
3
<% if yield(:title) %>
  <h1><%= yield(:title) %></h1>
<% end %>

To set the title, I call the title helper in my views like this:


<% title "User profile of #{@user.login}" %>

Forms

The tabindex attribute of form elements specifies the position in the tabbing order. The tabbing order defines the order in which elements will receive focus when navigated by the user via the keyboard.

Instead of specifying the tabindex yourself, you can use the helper method

1
2
3
4
def autotab
  @current_tab ||= 0
  @current_tab += 1
end

in your views like this:


<%= f.text_field :first_name, :tabindex => autotab %>

Ruby on Rails 2.0 added the very handy label form helper. But unfortunately there is no label_tag helper. So I added one myself:

1
2
3
4
5
6
7
# Creates a label tag.
#   label_tag('post_title', 'Title')
#     <label for="post_title">Title</label>
def label_tag(name, text=nil, options = {})
  content = (text.blank? ? nil : text.to_s) || name.to_s.humanize
  content_tag('label', content, { 'for' => name }.merge(options.stringify_keys))
end

I often use a submit button together with a link to cancel. Overriding the submit_tag helper provided by Rails, helps me to DRY up my views:

1
2
3
4
5
def submit_tag(value = "Save changes", options = {})
  or_opt = options.delete(:or)
  return super + " <span class='button_or'>" + "or" + " " + or_opt + "</span>" if or_opt
  super
end

Now I can use the following code in my views:


<%= f.submit 'Update', :or => link_to('Cancel', user_path(@user)), :tabindex => autotab %>

Markaby

Markaby lets you generate HTML for views by writing ordinary Ruby code. I particulary like to use Markaby in my helpers. To simplify the use of Markaby, I’m using the following helper method:

1
2
3
def markaby(&block)
  Markaby::Rails::Builder.new({}, self, &block) 
end

The markaby helper can be used in other helpers like this:

1
2
3
4
5
6
7
8
def logo_text
  markaby do
    span.logo do
      span.blue "My"
      span.green "App"
    end
  end
end

The above helper generates the following HTML code:


<span class="logo"><span class="blue">My</span><span class="green">App</span></span>

Object-Oriented Software Construction 0

Posted on Wednesday, November 28

All you need is code
Code is all you need

  – The bOOtles

(Source: Slides of a talk about the “Eiffel Method” by Betrand Meyer)

Debugging output macro for C 1

Posted on Thursday, July 05

In the ISO C standard of 1999, a macro can be declared to accept a variable number of arguments. This allows you for example to define the following debugging output macro:

1
2
3
4
5
6
7
8
9
/* Print debugging output, if DEBUG is defined */
#define DEBUG

/* Debugging output macro */
#ifdef DEBUG
#define debug(...)  { (void) fprintf (stdout, __VA_ARGS__); }
#else
#define debug(...)  ((void) 0)
#endif

To use the macro in your code, simply use it like printf:


debug("The Answer to the Ultimate Question is '%d'\n", answer);

It is of course possible to achieve the same behaviour with a function, where you could wrap the call to fprintf between ifdef DEBUG and endif. But this has the overhead of calling a function, whereas the preprocessor replaces every occurrence of debug(...) by ((void) 0) if DEBUG is undefined.

Instead of defining DEBUG inside your code, you could also the -DDEBUG preprocessor option of GCC.

Amber Scripting Language 0

Posted on Monday, July 02

While browsing EiffelZone for new Eiffel related releases, I stubled across Amber, an Eiffel-like scripting language.

The highlights of Amber are

  • Syntax and semantics somewhere between those of Ruby and Eiffel
  • Scales well to large systems, thanks to built-in support for Design by Contract
  • Concise syntax, easy to read and maintain
  • Dynamic typing

The Amber Scripting Language is still experimental. Nevertheless, the included hand-crafted examples and test cases do work. Following is the “99 Bottles of Beer” implementation of Amber:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
-- Prints the song "99 Bottles of Beer"
-- See http://99-bottles-of-beer.net/

99.down_to(1, agent(n) do
   print_line(bottles(n) & " on the wall, " & bottles(n) & ".")
   if n = 0 then
      print_line(
         "Go to the store and buy some more ... 99 bottles of beer."
      )
   else
      print("Take one down and pass it around, ")
      print_line(bottles(n - 1) & " on the wall.\n")
   end
end)

private
   bottles(n)
      is
         if n = 0 then
            "No more bottles of beer"
         elseif n = 1 then
            "1 bottle of beer"
         else
            n & " bottles of beer"
         end
      end
end

alt.lang.jre 0

Posted on Saturday, June 30

I found a very interesting series of articles about alternate languages for the JRE on IBM developerWorks.

The following languages were discussed:

While Jython, JRuby, Rhino and NetRexx are ports of existing languages to the JRE, Groovy and Nice are complete new language based entirely on the Java programming APIs.

Groovy blends some of the most useful features of Ruby, Python, and Smalltalk. But still conserves a core syntax based on the Java language.

Groovy supports the following features:

  • Dynamic types
  • Closures
  • Regex syntax similar to Perl
  • Native syntax for Lists and Maps
  • Operator Overloading to simplify working with datatypes Collections and Maps
  • Polymorphic iteration and Autoboxing
  • Providing a shell interpreter
  • Compiles straight to Java bytecode and works cleanly with all existing Java objects and libraries

All in all Groovy is exactly like Java should have been.

It’s nice (pun intended) to see how the JRE supports more and more alternate languages. The only language I still miss is JPerl. To by-pass the time waiting for it, I’ll play with Inline::Java and PLJava.

EiffelStudio 6.0 released 0

Posted on Wednesday, June 27

In mid-April, Eiffel Software announced a changed in its release cycle by having two EiffelStudio releases per year (one in mid-May and one in mid-November). This would really start this Fall (i.e Fall in the northern hemisphere) since for this Spring, it was decided to have a release by mid-June. I’m happy to report that we have been on time and released EiffelStudio 6.0 on June 19th.

For more information see Manu’s Eiffel blog.

"Perl 6 and Parrot Essentials" now open source 0

Posted on Wednesday, June 27

This book is an unparalleled sneak-peek of what’s coming in the widely-anticipated Perl 6. It uncovers groundbreaking new developments in Parrot – the interpreter engine that will execute code written in the new Perl 6 language and the most revolutionary change in the language itself – Apocalypse 12 on objects. It also includes expanded coverage of Apocalypse 5 (regular expressions) and Apocalypse 6 (subroutines).

“Perl 6 and Parrot Essentials” is now open source. A limited preview of the second edition is also available via Google Book Search.

me.goes :Camping 0

Posted on Monday, June 18

I was playing with Camping lately and thought I’d share some of the links I found with you: