digitalmars.D.learn - How to translate C# 'readonly' keyword into D
- o3o (56/56) Feb 04 2013 I'm a C# programmer, when I apply IoC pattern I use "readonly"
- simendsjo (8/65) Feb 04 2013 In D, everything accessible from const is also const. You
- simendsjo (23/91) Feb 04 2013 So this works:
- o3o (21/25) Feb 04 2013 Thanks simendsjo, now I get it...
- rumbu (6/31) Feb 04 2013 First, AFAIK, there is no equivalent of C# "readonly" in D,
- Jacob Carlborg (5/15) Feb 04 2013 The syntax for template instantiation is:
- Jacob Carlborg (14/39) Feb 04 2013 The closest would probably be defining a property with only a getter:
keyword (http://msdn.microsoft.com/en-us/library/acdd6hb7%28v=vs.71%29.aspx) in this manner: :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
Feb 04 2013
On Monday, 4 February 2013 at 09:02:31 UTC, o3o wrote:keyword (http://msdn.microsoft.com/en-us/library/acdd6hb7%28v=vs.71%29.aspx) in this manner: :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 () constIn 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.
Feb 04 2013
On Monday, 4 February 2013 at 10:26:55 UTC, simendsjo wrote:On Monday, 4 February 2013 at 09:02:31 UTC, o3o wrote: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() {}"readonly" keyword (http://msdn.microsoft.com/en-us/library/acdd6hb7%28v=vs.71%29.aspx) in this manner: :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 () constIn 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.
Feb 04 2013
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 Mock<IFoo>(); //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
Feb 04 2013
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 Mock<IFoo>(); //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
Feb 04 2013
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 Mock<IFoo>(); //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
Feb 04 2013
On 2013-02-04 10:02, o3o wrote:(http://msdn.microsoft.com/en-us/library/acdd6hb7%28v=vs.71%29.aspx) in this manner: :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
Feb 04 2013