How to translate C# 'readonly' keyword into D

2013-02-04 Thread o3o
I'm a C# programmer, when I apply IoC pattern  I use readonly 
keyword 
(http://msdn.microsoft.com/en-us/library/acdd6hb7%28v=vs.71%29.aspx) 
in this manner:


:// C# code
:interface IFoo {
:  void Fun();
:}
:
:class Foo: IFoo {
:  void Fun() {...}
:}
:class Bar {
:  private readonly IFoo foo;
:  // inject IFoo into Bar
:  Bar(IFoo foo) {
:// assert(foo != null);
:this.foo = foo;
:  }
:  void Gun() {
:// foo = new Foo();  ERROR: foo is readonly!
:foo.Fun();
:  }
:}

Can someone help me to translate readonly IFoo foo; so that the 
dmd compiler raises an error when I write foo = new Foo(); ?


I try:
:// D code
:interface IFoo {
:   void fun();
:}
:
:class Foo: IFoo {
:   void fun() {
:  writeln(fun...);
:   }
:}
:
:class Bar {
:   private const IFoo service;
:   this(const IFoo service) {
:  this.service = service;
:   }
:
:   void gun() {
:  service.fun();
:   }
:}
:unittest {
:  const(IFoo) s = new Foo;
:  auto bar = new Bar(s);
:  bar.gun();
:}
but the compiler complains:
Error: function main.IFoo.fun () is not callable using argument 
types () const




Re: How to translate C# 'readonly' keyword into D

2013-02-04 Thread simendsjo

On Monday, 4 February 2013 at 09:02:31 UTC, o3o wrote:
I'm a C# programmer, when I apply IoC pattern  I use readonly 
keyword 
(http://msdn.microsoft.com/en-us/library/acdd6hb7%28v=vs.71%29.aspx) 
in this manner:


:// C# code
:interface IFoo {
:  void Fun();
:}
:
:class Foo: IFoo {
:  void Fun() {...}
:}
:class Bar {
:  private readonly IFoo foo;
:  // inject IFoo into Bar
:  Bar(IFoo foo) {
:// assert(foo != null);
:this.foo = foo;
:  }
:  void Gun() {
:// foo = new Foo();  ERROR: foo is readonly!
:foo.Fun();
:  }
:}

Can someone help me to translate readonly IFoo foo; so that 
the dmd compiler raises an error when I write foo = new 
Foo(); ?


I try:
:// D code
:interface IFoo {
:   void fun();
:}
:
:class Foo: IFoo {
:   void fun() {
:  writeln(fun...);
:   }
:}
:
:class Bar {
:   private const IFoo service;
:   this(const IFoo service) {
:  this.service = service;
:   }
:
:   void gun() {
:  service.fun();
:   }
:}
:unittest {
:  const(IFoo) s = new Foo;
:  auto bar = new Bar(s);
:  bar.gun();
:}
but the compiler complains:
Error: function main.IFoo.fun () is not callable using argument 
types () const


In D, everything accessible from const is also const. You 
specifically state that you will not modify your IFoo instance. 
But then you call foo(). foo() in turn isn't marked as const, so 
this can modify IFoo, and thus break const.
So.. Every method you call through a const instance must also be 
const, otherwise you have the ability to change something that 
should be a constant.




Re: How to translate C# 'readonly' keyword into D

2013-02-04 Thread simendsjo

On Monday, 4 February 2013 at 10:26:55 UTC, simendsjo wrote:

On Monday, 4 February 2013 at 09:02:31 UTC, o3o wrote:
I'm a C# programmer, when I apply IoC pattern  I use 
readonly keyword 
(http://msdn.microsoft.com/en-us/library/acdd6hb7%28v=vs.71%29.aspx) 
in this manner:


:// C# code
:interface IFoo {
:  void Fun();
:}
:
:class Foo: IFoo {
:  void Fun() {...}
:}
:class Bar {
:  private readonly IFoo foo;
:  // inject IFoo into Bar
:  Bar(IFoo foo) {
:// assert(foo != null);
:this.foo = foo;
:  }
:  void Gun() {
:// foo = new Foo();  ERROR: foo is readonly!
:foo.Fun();
:  }
:}

Can someone help me to translate readonly IFoo foo; so that 
the dmd compiler raises an error when I write foo = new 
Foo(); ?


I try:
:// D code
:interface IFoo {
:   void fun();
:}
:
:class Foo: IFoo {
:   void fun() {
:  writeln(fun...);
:   }
:}
:
:class Bar {
:   private const IFoo service;
:   this(const IFoo service) {
:  this.service = service;
:   }
:
:   void gun() {
:  service.fun();
:   }
:}
:unittest {
:  const(IFoo) s = new Foo;
:  auto bar = new Bar(s);
:  bar.gun();
:}
but the compiler complains:
Error: function main.IFoo.fun () is not callable using 
argument types () const


In D, everything accessible from const is also const. You 
specifically state that you will not modify your IFoo instance. 
But then you call foo(). foo() in turn isn't marked as const, 
so this can modify IFoo, and thus break const.
So.. Every method you call through a const instance must also 
be const, otherwise you have the ability to change something 
that should be a constant.


So this works:
interface IFoo {
void fun() const;
}

class Foo : IFoo {
void fun() const {}
}

class Bar {
private const IFoo service;
this(const IFoo service) {
this.service = service;
}

void gun() {
service.fun();
}
}

unittest {
const s = new Foo();
auto bar = new Bar(s);
bar.gun();
}

void main() {}


Re: How to translate C# 'readonly' keyword into D

2013-02-04 Thread Jacob Carlborg

On 2013-02-04 10:02, o3o wrote:

I'm a C# programmer, when I apply IoC pattern  I use readonly keyword
(http://msdn.microsoft.com/en-us/library/acdd6hb7%28v=vs.71%29.aspx) in
this manner:

:// C# code
:interface IFoo {
:  void Fun();
:}
:
:class Foo: IFoo {
:  void Fun() {...}
:}
:class Bar {
:  private readonly IFoo foo;
:  // inject IFoo into Bar
:  Bar(IFoo foo) {
:// assert(foo != null);
:this.foo = foo;
:  }
:  void Gun() {
:// foo = new Foo();  ERROR: foo is readonly!
:foo.Fun();
:  }
:}

Can someone help me to translate readonly IFoo foo; so that the dmd
compiler raises an error when I write foo = new Foo(); ?


The closest would probably be defining a property with only a getter:

class Bar
{
private IFoo foo_;

private @property foo () { return foo_; }

this (IFoo foo)
{
foo_ = foo;
}
}

You can still change the foo_ variable.

--
/Jacob Carlborg


Re: How to translate C# 'readonly' keyword into D

2013-02-04 Thread o3o

On Monday, 4 February 2013 at 10:26:55 UTC, simendsjo wrote:

[cut]
So.. Every method you call through a const instance must also 
be const, otherwise you have the ability to change something 
that should be a constant.

Thanks simendsjo, now I get it...

So, let me continue the example (I remove const for 
simplicity)...

I would like check that bar.gun() call fun() function from IFoo

unittest {
 auto foo = new MockIFoo(); //Will not compile.Mock doesn't 
(yet) exist

 auto bar = new Bar(foo);
 bar.gun();
 foo.Received().fun(); // pass if 'fun' was called
}

void main() {}

In other words, I need a mock object like nsubstitute 
(http://nsubstitute.github.com/help/getting-started/) or moq 
(http://code.google.com/p/moq/)


In your old post 
http://forum.dlang.org/thread/il29hs$2svt$1...@digitalmars.com
you were asking for mocking frameworks...do you found any 
solution?

Thanks


Re: How to translate C# 'readonly' keyword into D

2013-02-04 Thread rumbu
First, AFAIK, there is no equivalent of C# readonly in D, 
despite the fact that D uses 3 keywords for various kinds of 
immutability.


Second, here you can find a  mocking library for D: 
http://www.dsource.org/projects/dmocks/wiki/DMocks




On Monday, 4 February 2013 at 13:35:24 UTC, o3o wrote:

On Monday, 4 February 2013 at 10:26:55 UTC, simendsjo wrote:

[cut]
So.. Every method you call through a const instance must also 
be const, otherwise you have the ability to change something 
that should be a constant.

Thanks simendsjo, now I get it...

So, let me continue the example (I remove const for 
simplicity)...

I would like check that bar.gun() call fun() function from IFoo

unittest {
 auto foo = new MockIFoo(); //Will not compile.Mock 
doesn't (yet) exist

 auto bar = new Bar(foo);
 bar.gun();
 foo.Received().fun(); // pass if 'fun' was called
}

void main() {}

In other words, I need a mock object like nsubstitute 
(http://nsubstitute.github.com/help/getting-started/) or moq 
(http://code.google.com/p/moq/)


In your old post 
http://forum.dlang.org/thread/il29hs$2svt$1...@digitalmars.com
you were asking for mocking frameworks...do you found any 
solution?

Thanks


Re: How to translate C# 'readonly' keyword into D

2013-02-04 Thread Jacob Carlborg

On 2013-02-04 14:35, o3o wrote:


So, let me continue the example (I remove const for simplicity)...
I would like check that bar.gun() call fun() function from IFoo

unittest {
  auto foo = new MockIFoo(); //Will not compile.Mock doesn't (yet)
exist
  auto bar = new Bar(foo);
  bar.gun();
  foo.Received().fun(); // pass if 'fun' was called
}

void main() {}


The syntax for template instantiation is:

auto foo = new Mock!(IFoo)();

--
/Jacob Carlborg