The interaction of encapsulation and properties in D

Jacob Carlborg doob at
Thu Jul 11 00:37:56 PDT 2013

On 2013-07-11 07:26, Jonathan M Davis wrote:

> 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.

I use public fields sometimes but as you say, there are problems. One 
can also run into problems with returning structs by value. The compiler 
would also need to do property rewriting to work flawlessly.

/Jacob Carlborg

More information about the Digitalmars-d mailing list