Bug 6707 – Error message for mismatch of const/non-const property functions needs to improve
Status
RESOLVED
Resolution
DUPLICATE
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2011-09-21T08:44:00Z
Last change time
2013-01-07T07:10:36Z
Keywords
diagnostic
Assigned to
nobody
Creator
andrej.mitrovich
Comments
Comment #0 by andrej.mitrovich — 2011-09-21T08:44:17Z
struct Foo
{
int payload;
@property void value(int x) { payload = x;}
@property int value() { return payload; }
const bool opEquals(ref const(Foo) other)
{
return this.value == other.value;
}
}
void main() { }
This errors with:
Error: function test.Foo.value (int x) is not callable using argument types ()
The solution is to add a const property function like this one:
@property const(int) value() const { return payload; }
But this can't be easily figured out from that error message.
Comment #1 by andrej.mitrovich — 2012-10-04T12:56:30Z
Better test case:
struct Foo
{
@property bool value() { return true; }
void test() const
{
auto x = value; // not callable using argument types ()
auto y = value(); // not callable using argument types () const
}
}
void main() { }
It seems 'this' for property func isn't printed out properly unless it's called as a function, I'll see if this is fixable.
Comment #2 by github-bugzilla — 2012-10-08T10:48:56Z
Comment #3 by andrej.mitrovich — 2012-10-19T20:57:42Z
*** Issue 8549 has been marked as a duplicate of this issue. ***
Comment #4 by bearophile_hugs — 2012-10-21T14:09:55Z
I reopen this as enhancement request because I think the error message is not good enough still. See Issue 8549 for more info.
This program:
struct Foo {
int[] opSlice() {
return [0];
}
}
struct Bar {
Foo spam;
const(Foo) bar() const { return spam; }
}
void main() {
Bar().bar()[];
}
Currently gives:
test.d(11): Error: function test.Foo.opSlice () is not callable using argument types () const
In Issue 8549 Andrej Mitrovic suggests an error message similar to:
Error: function test.Foo.opSlice () is not callable using const(this)
But I think a better error message, more newbie-friendly, tells the programmer how to fix the problem, (not complete error message):
test.d(11): Error: function test.Foo.opSlice() needs to be const to [...]
Comment #5 by andrej.mitrovich — 2012-10-21T14:15:14Z
(In reply to comment #4)
> But I think a better error message, more newbie-friendly, tells the programmer
> how to fix the problem, (not complete error message):
>
> test.d(11): Error: function test.Foo.opSlice() needs to be const to [...]
If it's in a 3rd party library then the user has no other choice but to use a const object. I don't think we should start recommending to rewrite code like that, especially to newbies.
Comment #6 by bearophile_hugs — 2012-10-21T14:33:36Z
(In reply to comment #5)
> If it's in a 3rd party library then the user has no other choice but to use a
> const object. I don't think we should start recommending to rewrite code like
> that, especially to newbies.
I understand.
Time ago I have seen this error message and it took me some some to understand what's the problem. So I've asked for a simpler to understand error message. If you think my suggestion is not good, then do something different :-)
Maybe we should ask to more people in the D newsgroup.
Comment #7 by andrej.mitrovich — 2012-12-10T08:57:39Z
*** Issue 9132 has been marked as a duplicate of this issue. ***
Comment #8 by andrej.mitrovich — 2012-12-27T17:43:17Z
The pull for Issue 1730 will likely resolve your issue of the unreadable error message.
*** This issue has been marked as a duplicate of issue 1730 ***
Comment #9 by k.hara.pg — 2013-01-07T07:10:36Z
*** Issue 8552 has been marked as a duplicate of this issue. ***