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

Reply via email to