D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4993 - Temporary values and opIndexAssign
Summary: Temporary values and opIndexAssign
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: pull, rejects-valid
: 5202 (view as issue list)
Depends on:
Blocks:
 
Reported: 2010-10-04 13:14 UTC by Simen Kjaeraas
Modified: 2012-03-17 21:55 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Simen Kjaeraas 2010-10-04 13:14:44 UTC
The following code does not compile. Note that the exact same functions are present in both the inner and outer structs, and that opIndex works for both.

struct Foo{
    struct Bar {
        int opIndex( size_t index ) {
            return 3;
        }
        
        void opIndexAssign( int value, size_t index ) {
        }
    }
    @property
    auto bar( ) {
        return Bar( );
    }
    
    int opIndex( size_t index ) {
        return 3;
    }
    
    void opIndexAssign( int value, size_t index ) {
    }
}

void main( ) {
    Foo f;
    f[3] = f[2];
    Foo.Bar b;
    b[3] = b[2];
    f.bar[3] = f.bar[2]; // Fails
}


foo.d(28): Error: f.bar()[3u] is not an lvalue
Comment 1 Alvaro 2012-03-05 13:56:35 UTC
Note that this works instead:

  f.bar()[3]= f.bar[2];

if @property is not enforced. It's the same temporary struct returned, but without properties.

Fixing this would allow implementing "indexed properties" as long as D does not provide them natively. i.e, things like:

  image.pixels[x,y] = Rgb(0,0,0);
  treeViewItem.text[2] = "text for column 2 in this item";
Comment 2 Nils 2012-03-06 23:34:22 UTC
*** Issue 5202 has been marked as a duplicate of this issue. ***