On Wed, 19 Jun 2013 16:34:03 +0100, Szymon Gatner <noem...@gmail.com> wrote:

On Wednesday, 19 June 2013 at 14:04:35 UTC, Regan Heath wrote:
On Wed, 19 Jun 2013 12:01:02 +0100, Szymon Gatner <noem...@gmail.com> wrote:
D is the only language (that I am aware of) that has first class unit testing support. What do you think? Do we really just "mentally masturbate"?

I'm more interested in whether DCI is doable/natural in D.. that seems like an interesting idea.

R

I actually learned about DCI from that very presentation and am
wondering the same thing.

So.. to me it seems the basic idea is that you have your object i.e. BankAccount which is a dumb/data object with few methods (perhaps just properties). Then, depending on the use-case it participates in, it takes on a role (at run time) which carries with it some methods.

So.. something like?

import std.stdio;

// Dumb/data object
class BankAccount
{
public:
  string name;
  int balance;

  this(string _name, int _balance)
  {
    name = _name;
    balance = _balance;
  }
}

// Roles..
class SourceAccount(T)
{
private:
  T account;

public:
  alias account this;

  this(T _account)
  {
    account = _account;
  }

  void TransferTo(TargetAccount!BankAccount target, int amount)
  {
    target.balance += amount;
    balance -= amount;
  }
}

class TargetAccount(T)
{
private:
  T account;

public:
  alias account this;

  this(T _account)
  {
    account = _account;
  }
}

// Use case..
void TransferFunds(BankAccount sa, BankAccount ta, int amount)
{
  auto source = new SourceAccount!BankAccount(sa);
  auto target = new TargetAccount!BankAccount(ta);
  source.TransferTo(target, amount);
}

// Logic..
void main()
{
  auto savings = new BankAccount("savings", 10000);
  auto current = new BankAccount("current", 250);
        
  writefln("Current balances: ");
  writefln("  %s: %d", savings.name, savings.balance);
  writefln("  %s: %d", current.name, current.balance);
        
  writefln("");
  writefln("Transfer funds..");
  TransferFunds(savings, current, 500);
        
  writefln("");
  writefln("New balances: ");
  writefln("  %s: %d", savings.name, savings.balance);
  writefln("  %s: %d", current.name, current.balance);
}

R
--
Using Opera's revolutionary email client: http://www.opera.com/mail/

Reply via email to