Calling delegate properties without parens
Artur Skawina
art.08.09 at gmail.com
Sat Apr 14 13:28:55 PDT 2012
On 04/14/12 20:47, Piotr Szturmaj wrote:
> I have following code:
>
> import std.array, std.range, std.stdio;
>
> struct CommonInputRange(E)
> {
> @property bool delegate() empty;
> @property E delegate() front;
> void delegate() popFront;
> }
>
> void main(string[] args)
> {
> alias CommonInputRange!dchar DCRange;
> static assert(isInputRange!DCRange);
> DCRange dc;
> auto dcr = "abcdefg";
> auto t = dcr.takeExactly(3);
> dc.empty = &t.empty;
> dc.front = &t.front;
> dc.popFront = &t.popFront;
>
> for ( ; !dc.empty(); dc.popFront())
> writeln(dc.front());
> }
>
> As you can see in the for loop, range primitives must be called using parens (), otherwise they don't work.
>
> Do you know if there are plans to implement @property for delegates and function pointers?
@property is for functions masquerading as data, i'm not sure extending it
to pointers and delegates would be a good idea. What you are asking for is
basically syntax sugar for:
struct CommonInputRange(E)
{
bool delegate() _empty;
@property auto empty() { return _empty(); };
@property auto empty(typeof(_empty) dg) { _empty = dg; };
E delegate() _front;
@property auto front() { return _front(); };
@property auto front(typeof(_front) dg) { _front = dg; };
void delegate() popFront;
}
// [1]
artur
[1] which could also /almost/ be expressed as:
struct PropDeleg(T) {
T dg;
@property auto get() { return dg(); };
alias get this;
void opAssign(T d) { dg = d; };
}
struct CommonInputRange(E)
{
PropDeleg!(bool delegate()) empty;
PropDeleg!(E delegate()) front;
void delegate() popFront;
}
except this one would need an (implicit) conversion to get the same
behavior. IOW it should work everywhere, as long as the result is
assigned to a different type; ie you'd need
"{ dchar c = dc.front; writeln(c); }" in the above example, because
otherwise the writeln template will accept the struct, but never
really use it, so the dchar conversion does not happen.
More information about the Digitalmars-d-learn
mailing list