[NAME]
ALL.misc.comparison.codesect

[TITLE]
Code Block Method (Ruby)

[DESCRIPTION]

Ruby supports a special type of functions called code block method, which can accept a 
block of codes as an additional and implicit parameter. Dao also supports such method in
a similar way, and such method is often called code section method in Dao, that's 
because there is a significant difference between the implementation of such methods in
Dao and in Ruby.

In Ruby, the code block attached to a call is compiled and passed to the call as a
closure. But in Dao, the code block is compile as a part (section) of the host (caller)
function, and is not passed to the call. Instead, the call will locate the right code
section in the caller and execute that section when necessary. There are advantages and
disadvantages of both implementation, but the implementation as code sections should be
more efficient.

The following examples should demonstrate some syntactic differences.
     
   1  # Ruby
   2  def CodeBlock
   3      puts 'Starting CodeBlock'
   4      yield 123
   5      puts 'Ending CodeBlock'
   6  end
   7  
   8  CodeBlock { |x| puts "#{x}" }
     

     
   1  # Dao
   2  routine CodeBlock()
   3  [ int ] # parameter type for the block;
   4  {
   5      io.writeln( 'Starting CodeBlock' )
   6      yield( 123 )
   7      io.writeln(  'Ending CodeBlock' )
   8  }
   9  
  10  CodeBlock { [x] io.writeln( x ) }
     

Note: code section method is not really inspired by Ruby, but it was evolved from the
previous functional method in Dao. Only the syntax was eventually settled down and
converged to something similar to Ruby code block method, whose syntax seems quite
appropriate for such feature.