Why are there Properties in D?

Robin robbepop at web.de
Fri Feb 14 06:55:01 PST 2014


Hiho,

first of all, thank you all for your fast responses.

On Friday, 14 February 2014 at 11:34:00 UTC, Marc Schütz wrote:
> I'd like to add generic code. For an example, look at ranges: 
> their `front` and `empty` must be callable without parens. This 
> makes it possible for some ranges to have a normal member 
> variable `front`, or a static enum member `empty` (which can 
> even be tested for at compile time!), and for others to use 
> methods/UFCS functions instead. Without these, a lot of the 
> generic algorithms in `std.algorithm` would be full of 
> `is(typeof(range.empty)) || is(typeof(range.empty()))`, or 
> similar tests, making them harder to read and get right.

As I said I am still learning D (and I think I will never stop 
learning it xD) so sorry if I misunderstand certain programming 
patterns.

As far as I can imagine you could also implement ranges via front 
and empty functions and ranges could easily expose their 
variables as a getter method named front() or empty() and nobody 
would care if it is handled functionally or via a simple variable 
again.

I don't get why all the algorithms would be full of 
is(typeof(range.empty) || is(typeof(range.empty()) with the 
approach of getter methods for front and empty or am I wrong?

Robin


More information about the Digitalmars-d mailing list