When should I make property functions?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon Nov 19 21:13:51 UTC 2018


On Monday, November 19, 2018 1:52:20 PM MST 12345swordy via Digitalmars-d 
wrote:
> On Monday, 19 November 2018 at 20:36:42 UTC, Jonathan M Davis
>
> wrote:
> > [...]
>
> Expect for the fact that they can't do binary assignment.
> https://github.com/dlang/DIPs/pull/97

Well, that's kind of my point. Property functions do not act the same way as
member variables, and ultimately they can't, because they're fundamentally
different. There may or may not be further language enhancements to make
them more syntactically similar, but ultimately, a property function is
_not_ a member variable, and you will never be able to just swap out a
public member variable for a property function and expect all code using it
to just continue to work. So, in general, you should not make your member
variables public with the idea the you might then later turn them into
property functions when refactoring if it turns out that you want them to be
more than simple getters ands setters. This is especially true if you're
writing code that you're providing to other people rather than writing code
that you're in complete control of and therefore can fully update if/when it
breaks if you later decide to change a member variable into a property
function.

- Jonathan M Davis





More information about the Digitalmars-d mailing list