Why are there Properties in D?
Marc Schütz" <schuetzm at gmx.net>
Marc Schütz" <schuetzm at gmx.net>
Fri Feb 14 03:33:58 PST 2014
It's true that it hides what happens behind the scenes, but there
are several advantages. Rikki Cattermole already mentioned
instrumentation; more generally, this makes it easy to change
between getter/setter and member variable without modifying all
the use sites.
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.
More information about the Digitalmars-d
mailing list