Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
May 16, 2014 idup class | ||||
---|---|---|---|---|
| ||||
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 Re: idup class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joshua Niehus | 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 Re: idup class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joshua Niehus | 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 Re: idup class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Fri, 16 May 2014 16:36:36 -0400, Ali Çehreli <acehreli@yahoo.com> wrote: Beat you by 8 seconds :) -Steve |
May 16, 2014 Re: idup class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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 Re: idup class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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 :)
|
Copyright © 1999-2021 by the D Language Foundation