D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4690 - ref return should allow assignment if not overridden
Summary: ref return should allow assignment if not overridden
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Windows
: P2 major
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2010-08-20 06:39 UTC by David Simcha
Modified: 2011-08-12 20:41 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description David Simcha 2010-08-20 06:39:19 UTC
struct Foo {
    uint num;

    @property ref uint front() {
        return num;
    }

    ref uint opIndex(size_t index) {
        return num;
    }
}

void main() {
    Foo foo;
    foo.front++;       // Works
    foo.front += 1;    // error 1
    foo.front = 1;     // error 2
    foo[0] = 1;        // error 3
}

If something returns by reference then it should be usable for assignment, though if the class/struct contains an explicit setter property or opIndexAssign, this should override assignment via ref return.  Here are the error messages produced.

test9.d(16): Error: 'foo.front' is not a scalar, it is a @property ref uint()
test9.d(16): Error: incompatible types for ((foo.front) += (1)): '@property ref uint()' and 'int'
test9.d(17): Error: function test9.Foo.front () is not callable using argument types (int)
test9.d(17): Error: expected 0 arguments, not 1 for non-variadic function type @property ref uint()
test9.d(18): Error: operator [] assignment overload with opIndex(i, value) illegal, use opIndexAssign(value, i)

I'm aware that other bugs have reported bits and pieces of this problem, but I think this is a clearer bug report on the more general issue.  Also, marking as major because it's a significant impedement to library development in std.range.
Comment 1 David Simcha 2011-08-12 20:41:34 UTC
Can't reproduce this anymore.  Must have been fixed a long time ago.