idup class

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 16 13:36:36 PDT 2014


On 05/16/2014 01:28 PM, Joshua Niehus wrote:

 > trying to follow:
 > http://ddili.org/ders/d.en/class.html
 >
 > //--- OSX 10.9 DMD 2.065
 > module test;
 >
 > class Foo {
 >       int num;
 >
 >       this(int num) {
 >           this.num = num;
 >       }
 >
 >       Foo dup() const {
 >           return new Foo(this.num);
 >       }
 >
 >       immutable(Foo) idup() const {
 >           return new immutable(Foo)(this.num);
 >       }
 > }
 >
 > void main() {
 >       auto  foo = new Foo(1);
 >       auto mfoo = foo.dup();
 >       auto ifoo = foo.idup();
 > }
 >
 > * test.d(15): Error: mutable method test.Foo.this is not callable
 > using a immutable object
 > * test.d(15): Error: no constructor for Foo
 > * Failed: ["dmd", "-v", "-o-", "test.d", "-I."]
 > //---
 >
 > What am i missing?

My apologies. The code was written for an older version of dmd. The 
simplest fix is to define the constructor as pure:

      pure this(int num) {
          this.num = num;
      }

Ali



More information about the Digitalmars-d-learn mailing list