For a D range, should front() be a @property?

Viewed 50

When I define my D range, should front() be a @property? (or just a "regular" member function?)

1 Answers

Yes, it is supposed to be (the formal definition is under the isInputRange template in the docs, see: http://dpldocs.info/experimental-docs/std.range.primitives.isInputRange.html ).

In practice, it may work anyway, but since the docs say it and typeof(range.front) is a common check in range constraints and that is the one thing @property will actually change in the dmd implementation right now, it is best to use it to be compliant.

Now, @property currently does not prohibit calling it like range.front(), with parenthesis, you should NOT do that, since at some point in the future, @property might change to block that behavior.

So yeah, use @property on range.front and always use it as a property for maximum forward compatibility.

Related