www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3511] New: ref return property confused with property setter

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3511

           Summary: ref return property confused with property setter
           Product: D
           Version: 2.036
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: k-foley onu.edu


--- Comment #0 from Kyle Foley <k-foley onu.edu> 2009-11-14 12:56:09 PST ---
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".

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 14 2009
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3511


Witold Baryluk <baryluk smp.if.uj.edu.pl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |baryluk smp.if.uj.edu.pl


--- Comment #1 from Witold Baryluk <baryluk smp.if.uj.edu.pl> 2010-02-05
05:46:48 PST ---
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?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 05 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3511



--- Comment #2 from Kyle Foley <k-foley 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: -------
Feb 05 2010