Bug 3511 – ref return property confused with property setter
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Linux
Creation time
2009-11-14T12:56:00Z
Last change time
2015-06-09T01:27:05Z
Keywords
rejects-valid
Assigned to
nobody
Creator
k-foley
Comments
Comment #0 by k-foley — 2009-11-14T12:56:09Z
import std.stdio;
struct A
{
private int _prop = 42;
ref int property() { return _prop; }
//void property(in int rhs) { _prop = rhs; }
}
int main(string[] args)
{
A a;
writeln( a.property );
a.property = 23; // produces the error below
/*
Error: function refProperty.A.property () is not callable using
argument types (int)
Error: expected 0 arguments, not 1 for non-variadic function type
ref int()
*/
writeln( a.property );
return 0;
}
---
I think this is a bug, but it may actually be a "feature".
Comment #1 by witold.baryluk+d — 2010-02-05T05:46:48Z
Yes, this is interesting "feature" but I think it should be marked to be so, be some kind of attributed:
struct A {
private int x_ = 42;
@property
@ref_getter_as_setter
ref int x() { return x_; }
}
It still can be allowed without this properties, but then compile should emit warning.
What do you think?
Comment #2 by k-foley — 2010-02-05T14:30:22Z
(In reply to comment #1)
> Yes, this is interesting "feature" but I think it should be marked to be so, be
> some kind of attributed:
>
>
> struct A {
> private int x_ = 42;
>
> @property
> @ref_getter_as_setter
> ref int x() { return x_; }
> }
>
> It still can be allowed without this properties, but then compile should emit
> warning.
>
> What do you think?
I think that properties in general suck. The only thing I want is the syntax for omitting the parentheses when calling a function without arguments. I don't want the compiler transforming something like "a.property = 42;" into "a.property(42);". It seems so bizarre to me, but I must be in the minority.
I would solve this problem by either introducing reference types like C++ or allowing alias this to alias to a dereferenced pointer:
struct Ref(T) {
T* _ref;
alias *_ref this;
this(ref T rhs) { _ref = &rhs; }
}
Then I could implement properties by doing something like this:
struct A
{
private int _prop = 42;
PropertyHelper property() { return PropertyHelper(_prop); }
struct PropertyHelper {
private int _p;
private Ref!int _reference;
alias _p this; // aliased to a copy
this(ref int rhs)
: _reference(rhs); // meh, I don't know how to do this in D
{
writeln("Log: called the getter");
_p = rhs; // note this is a copy
}
void opAssign(in int rhs)
{
writeln("Log: called the setter");
_reference = rhs;
}
}
}
But a problem could be that when doing "auto x = a.property;" the type of x would be PropertyHelper. I mean, personally I would just do int getProperty(); and void setProperty(int); and be done with it.
Anyways, I think we need to provide more general functionality from the compiler so that cool things can be done with libraries (e.g. letting alias this do more like the example above).
Comment #3 by k.hara.pg — 2011-06-18T01:01:45Z
*** Issue 5681 has been marked as a duplicate of this issue. ***