digitalmars.D.learn - idup class
- Joshua Niehus (27/27) May 16 2014 trying to follow:
- Steven Schveighoffer (20/47) May 16 2014 your constructor needs the immutable tag.
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (7/34) May 16 2014 My apologies. The code was written for an older version of dmd. The
- Steven Schveighoffer (3/4) May 16 2014 Beat you by 8 seconds :)
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (3/6) May 16 2014 I am happy to be within one minute. :)
- Joshua Niehus (3/9) May 16 2014 Ahh great thanks guys.
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
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
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
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
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 :) -SteveI am happy to be within one minute. :) Ali
May 16 2014
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









"Steven Schveighoffer" <schveiguy yahoo.com> 