www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - idup class

reply "Joshua Niehus" <jm.niehus gmail.com> writes:
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?
May 16 2014
next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 16 May 2014 16:28:41 -0400, Joshua Niehus <jm.niehus gmail.com>  
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?
your constructor needs the immutable tag. this(int num) immutable ... However, you can avoid much of this by tagging things as pure: (untested, but just keep the internals the same) class Foo { int num; this(int num) pure {...} Foo dup() const pure {...} immutable(Foo) idup() const pure {...} } void main() { ... // same implementation } Note that doing this, you do not need to have an idup, this should work: immutable ifoo = foo.dup(); -Steve
May 16 2014
prev sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
May 16 2014
next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 16 May 2014 16:36:36 -0400, Ali =C3=87ehreli <acehreli yahoo.com=
 wrote:
Beat you by 8 seconds :) -Steve
May 16 2014
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 05/16/2014 01:37 PM, Steven Schveighoffer wrote:
 On Fri, 16 May 2014 16:36:36 -0400, Ali Çehreli <acehreli yahoo.com> wrote:

 Beat you by 8 seconds :)

 -Steve
I am happy to be within one minute. :) Ali
May 16 2014
prev sibling parent "Joshua Niehus" <jm.niehus gmail.com> writes:
On Friday, 16 May 2014 at 20:36:37 UTC, Ali Çehreli wrote:
 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
Ahh great thanks guys. No worries Ali, great book, i refer to it all the time :)
May 16 2014