ARAVINDA VISHWANATHAPURA

Ruby-style blocks in Dlang

Mar 24, 2024
1 minute read.
ruby crystal dlang

Blocks are one of the powerful and most liked feature in Ruby and Crystal. The syntax is very intuitive and easy to understand. Below example shows a Ruby/Crystal block that remembers the options and sets the original options before exiting the block.

ctx = Context.new
ctx.options.color = "blue"

ctx.saved_state do
  ctx.options.color = "red"
  show_alert(ctx.options)
end

# Below code prints "blue"
puts ctx.options.color

The code in-between do and end is block. Blocks are basically closure functions and this feature is available in many programming languages. In D, it is very easy to do using delegates. Equivalent to above code block looks like in D as below.

auto ctx = new Context();
ctx.options.color = "blue";

savedState({
  ctx.options.color = "red";
  showAlert(ctx.options);
});

// Below code prints "blue"
writeln(ctx.options.color);

Interesting? Lets see the definitions of the saved state in both the languages.

Ruby
class Options
  attr_accessor :color, :font_size
end

class Context
  attr_accessor :options

  def initialize
    @options = Options.new
  end

  def saved_state(&block)
    tmp = @options.dup
    block.call
    @options = tmp.dup
  end
end
Dlang
struct Options
{
  string color;
  int fontSize;
}

class Context
{
    Options options;

    void savedState(void delegate() func)
    {
        auto tmp = this.options;
        func();
        this.options = tmp;
    }
}

About Aravinda Vishwanathapura

Co-Founder & CTO at Kadalu Technologies, Creator of Sanka, Creator of Chitra, GlusterFS core team member, Maintainer of Kadalu Storage
Contact: Linkedin | Twitter | Facebook | Github | mail@aravindavk.in