property-like data members

Steven Schveighoffer schveiguy at yahoo.com
Mon Jan 3 07:27:17 PST 2011


On Mon, 03 Jan 2011 09:56:30 -0500, spir <denis.spir at gmail.com> wrote:

> On Mon, 03 Jan 2011 08:34:42 -0500
> "Steven Schveighoffer" <schveiguy at yahoo.com> wrote:
>
>> Yes, just use a data member:
>>
>> struct MyRange {
>>     int front;
>>     bool empty;
>>     void popFront();
>> }
>>
>> A property is actually supposed to work just like a field.
>>
>> There is no need for new syntax.
>
> Hum, does not work by me (else I would not have posted ;-)
> The compiler rejects the code complaining for missing opApply (which I  
> interpret as meaning it does not recognize a range in such an  
> interface). indeed, it works if manually implement iteration like for  
> instance:
> 	while (! coll.empty) {
> 	    auto element = coll.front;
> 	    use(element);
> 	    coll.popFront();
> 	}
> But then there no property in play (I mean the compiler does not expect  
> a property set implementing a range).

That's a bug.  isInputRange!S returns true.

There's nothing in the spec that says foreach requires those elements to  
be functions.  In fact, empty *does* work as a normal field, i.e. this  
struct is foreachable:

struct S
{
    @property int front() {return 0;}
    bool empty;
    void popFront() {empty = true;}
}

Filed:

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

-Steve


More information about the Digitalmars-d mailing list