www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - immutable method not callable using argument types () - doesn't make

reply "Daniel Donnelly" <enjoysmath gmail.com> writes:
I have: [code]

         module A;

	interface B {
	   public:
		  immutable B dup();
	}

	class A : B {
	   public:
		  this() {}
		  this(in char[] field) { this.field = field.dup; }
		  immutable A dup() { return new A(field); }
	   private:
		  char[] field;
	}

	void main()
	{
		B f, g;
		f = new A;
		g = new A;
		
		f = g.dup;
	}
[/code]

Compile: dmd A

Compile Output:
A.d(23): Error: function A.B.dup () immutable is not callable 
using argument types ()
Mar 27 2012
next sibling parent reply "Daniel Donnelly" <enjoysmath gmail.com> writes:
The two solutions are:

inout(A) dup() inout { ... }
A dup() const { ... }

I'm using the latter.  Basically all I need is to copy any 
object: const, immut-, or mutable.
Mar 27 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 02/07/2013 12:13 AM, Lee Braiden wrote:
 On Tuesday, 9 October 2012 at 22:47:16 UTC, denizzzka wrote:
 On Tuesday, 27 March 2012 at 12:57:18 UTC, bearophile wrote:
 Steven Schveighoffer:

 So what the compiler is saying is that you can't call dup with
 arguments (), you must call it with arguments '() immutable',
 meaning you must call it on an immutable B, not a mutable B.

Any space for compiler error message improvements here?

+1, spent two hours with a similar problem

+2. I wasted a little time on this too. Not much, but frustratingly more than necessary. Why someone would write that error message makes SOME sense when explained, but the error message itself is still non-sensical ;)

I am writing a D compiler front end, and currently the following message is displayed for the example: A.d:22:9: error: receiver for member function 'dup' is unqualified, but 'immutable' is required f = g.dup; ^~~~~ Is this what you'd want?
Feb 06 2013
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 27 Mar 2012 04:49:57 -0400, Daniel Donnelly <enjoysmath gmail.com>  
wrote:

 The two solutions are:

 inout(A) dup() inout { ... }

This transfers the constancy from the object to the result. Is that what you want? For arrays, dup means "return mutable", no matter what constancy it is called on.
 I'm using the latter.  Basically all I need is to copy any object:  
 const, immut-, or mutable.

BTW, here is what the compiler is complaining about: immutable B dup(); This is a member function, with the attribute immutable. What this does is specify that the hidden 'this' pointer is immutable. It's equivalent to this: B dup(immutable(B) this); So what the compiler is saying is that you can't call dup with arguments (), you must call it with arguments '() immutable', meaning you must call it on an immutable B, not a mutable B. -Steve
Mar 27 2012
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Steven Schveighoffer:

 So what the compiler is saying is that you can't call dup with 
 arguments (), you must call it with arguments '() immutable', 
 meaning you must call it on an immutable B, not a mutable B.

Any space for compiler error message improvements here? Bye, bearophile
Mar 27 2012
prev sibling next sibling parent "denizzzka" <4denizzz gmail.com> writes:
On Tuesday, 27 March 2012 at 12:57:18 UTC, bearophile wrote:
 Steven Schveighoffer:

 So what the compiler is saying is that you can't call dup with 
 arguments (), you must call it with arguments '() immutable', 
 meaning you must call it on an immutable B, not a mutable B.

Any space for compiler error message improvements here?

+1, spent two hours with a similar problem
Oct 09 2012
prev sibling next sibling parent "Lee Braiden" <leebraid gmail.com> writes:
On Tuesday, 9 October 2012 at 22:47:16 UTC, denizzzka wrote:
 On Tuesday, 27 March 2012 at 12:57:18 UTC, bearophile wrote:
 Steven Schveighoffer:

 So what the compiler is saying is that you can't call dup 
 with arguments (), you must call it with arguments '() 
 immutable', meaning you must call it on an immutable B, not a 
 mutable B.

Any space for compiler error message improvements here?

+1, spent two hours with a similar problem

+2. I wasted a little time on this too. Not much, but frustratingly more than necessary. Why someone would write that error message makes SOME sense when explained, but the error message itself is still non-sensical ;)
Feb 06 2013
prev sibling parent Lee Braiden <leebraid gmail.com> writes:
On Thu, 07 Feb 2013 00:31:20 +0100, Timon Gehr wrote:
 A.d:22:9: error: receiver for member function 'dup' is unqualified, but
 'immutable' is required
      f = g.dup;
          ^~~~~
 
 Is this what you'd want?

Almost, it's definitely better, but what's wrong with: "immutable object method dup cannot be called on mutable object d" ? ;) -- Lee
Feb 07 2013