The problem with D's properties

Mike Franklin slavo5150 at yahoo.com
Thu May 31 03:43:57 UTC 2018


On Thursday, 31 May 2018 at 01:49:42 UTC, DigitalDesigns wrote:
> https://dpaste.dzfl.pl/6c6e614b58ac
>
> The problem given in the list, is that a ref getter is used 
> when a setter does not exist. Why have a setter then?
>
> One problem is that if the setter has different behavior than 
> the getter there will be problems.
>
> The other problem is that += modifies the value.
>
> normally getters are thought to be immutable in the since that 
> they just return a value.
>
> but
>
> a += will modify what a points to.
>
> Using setters, we might want to trigger an event when the value 
> changes. But since the getter is used and can modify the event 
> won't be triggered.
>
> This is a bad design.
>
> any modifying operator should call the setter. This is 
> precisely what "set mean".
>
>
> a += b is suppose to be syntactic sugar for a = a + b, yet we 
> get different behavior. In one case only a getter is called and 
> in the second case the getter and then the setter.
>
> How D currently handles this is wrong and error prone. Either 
> depreciate the use of op assignments using properties or fix 
> it! lvalues should ALWAYS use the setter, it makes no sense 
> otherwise.

In case you're not already aware, there is a DIP in progress to 
remedy this issue:  https://github.com/dlang/DIPs/pull/97.  A few 
bugs have already been filed on this, and the DIP intends to 
address that.  If you have anything to add, please voice your 
thoughts in the DIP's PR.

Mike


More information about the Digitalmars-d mailing list