The interaction of encapsulation and properties in D

Jonathan M Davis jmdavisProg at gmx.com
Wed Jul 10 22:26:09 PDT 2013


On Thursday, July 11, 2013 04:55:09 =?UTF-8?B?Ikx1w61z?=.Marques 
<luismarques at gmail.com>@puremagic.com wrote:
> So, summing it up: even assuming that performance is not an
> issue, does the advice to always encapsulate your member
> variables (as one would do, for instance, in idiomatic Java)
> actually make sense for D, or would you recommend using public
> member variables when that is more straightforward, and the
> indirection is not yet needed?

In general, I would strongly advise against using public fields if you ever 
might want to replace them with property functions. The reason for this is 
that property functions really don't do all that great a job of emulating 
variables. For instance, taking the address of a variable gives you a 
completely different type than taking the address of a function does, and 
variables can be passed by ref, whereas the result of a property function 
can't (unless it returns by ref, which would ruin the encapsulation that it 
provides). In general, if you use public variables, you're just going to cause 
yourself trouble in the long run unless they stay public variables forever.

- Jonathan M Davis


More information about the Digitalmars-d mailing list