[Issue 3511] ref return property confused with property setter

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Feb 5 14:30:23 PST 2010


http://d.puremagic.com/issues/show_bug.cgi?id=3511



--- Comment #2 from Kyle Foley <k-foley at onu.edu> 2010-02-05 14:30:22 PST ---
(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).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list