[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 b
lock 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 t
here 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 closur
e. But in Dao, the code block is compile as a part (section) of the host (caller) functio
n, 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 disadvantage
s of both implementation, but the implementation as code sections should be more efficien
t.

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() [ int ]  # parameter type for the block;
   3  {
   4      io.writeln( 'Starting CodeBlock' )
   5      yield( 123 )
   6      io.writeln(  'Ending CodeBlock' )
   7  }
   8  
   9  CodeBlock { [x] io.writeln( x ) }
     

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