Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
March 27, 2012 immutable method not callable using argument types () - doesn't make sense | ||||
---|---|---|---|---|
| ||||
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 () |
March 27, 2012 Re: immutable method not callable using argument types () - doesn't make sense | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Donnelly | 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. |
March 27, 2012 Re: immutable method not callable using argument types () - doesn't make sense | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Donnelly | 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 |
March 27, 2012 Re: immutable method not callable using argument types () - doesn't make sense | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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
|
October 09, 2012 Re: immutable method not callable using argument types () - doesn't make sense | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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
|
February 06, 2013 Re: immutable method not callable using argument types () - doesn't make sense | ||||
---|---|---|---|---|
| ||||
Posted in reply to denizzzka | 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 ;)
|
February 06, 2013 Re: immutable method not callable using argument types () - doesn't make sense | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lee Braiden | 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?
|
February 07, 2013 Re: immutable method not callable using argument types () - doesn't make sense | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | 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 |
Copyright © 1999-2021 by the D Language Foundation