Thread overview
RFC: Implementation of binary assignment operators (e.g s.x += 2) for @property functions
Aug 15, 2017
ketmar
Aug 15, 2017
Johannes Pfau
Aug 15, 2017
Gary Willoughby
Aug 15, 2017
Johannes Pfau
Aug 15, 2017
Moritz Maxeiner
Aug 15, 2017
Jeremy DeHaan
Aug 15, 2017
Johan Engelen
August 15, 2017
An implementation of binary assignment operators for @property functions has been submitted to the DMD pull request queue at https://github.com/dlang/dmd/pull/7079.  It addresses the following issues:

Issue 8006 - Implement proper in-place-modification for properties
https://issues.dlang.org/show_bug.cgi?id=8006
------------------------------------------------------------------
This is the primary issue motivating the pull request.  The pull request is only a partial resolution of this issue; it implements binary assignment operators (e.g. `s.x += 2`), but not unary assignment operators (e.g. `s.x++`).  The plan is to implement unary assignment operators in a separate pull request after the fate of the binary assignment operators pull request is decided.

Issue 18187 – std.bitmap fields should be lvalues
https://issues.dlang.org/show_bug.cgi?id=16187
-------------------------------------------------
This is a more real-world example where binary assignment operators would be beneficial, as bit fields are not individually addressable.

Issue 15231 - opOpAssign allows assignment to rvalue
https://issues.dlang.org/show_bug.cgi?id=15231
----------------------------------------------------
While the pull request does not actually resolve the error illustrated in this issue, it demonstrates how a user desired binary assignment operators for @property functions so much that (s)he attempted to implement them indirectly by modeling the value type as a struct and overloading its operators.  Had binary assignment operators been implemented the user probably would not have had any need expend such effort.

Like the other users that commented in the issues above, I was a bit dismayed to discover that binary assignment operators were not implemented for @property functions.  When I submitted the pull request I was under the assumption that the only reason they weren’t implemented was because noone had gotten around to it yet.

Walter has requested that I make this forum post to inform the community of this change in an effort to discuss its merits and/or shortcomings.

We ask for your comments whether they be in approval or disapproval of this pull request so we can determine the best way forward.

Thank you,
Michael V. Franklin
August 15, 2017
Michael V. Franklin wrote:

> An implementation of binary assignment operators for @property functions has been submitted to the DMD pull request queue at https://github.com/dlang/dmd/pull/7079.  It addresses the following issues:
>
> Issue 8006 - Implement proper in-place-modification for properties
> https://issues.dlang.org/show_bug.cgi?id=8006
> ------------------------------------------------------------------
> This is the primary issue motivating the pull request.  The pull request is only a partial resolution of this issue; it implements binary assignment operators (e.g. `s.x += 2`), but not unary assignment operators (e.g. `s.x++`).  The plan is to implement unary assignment operators in a separate pull request after the fate of the binary assignment operators pull request is decided.
>
> Issue 18187 – std.bitmap fields should be lvalues
> https://issues.dlang.org/show_bug.cgi?id=16187
> -------------------------------------------------
> This is a more real-world example where binary assignment operators would be beneficial, as bit fields are not individually addressable.
>
> Issue 15231 - opOpAssign allows assignment to rvalue
> https://issues.dlang.org/show_bug.cgi?id=15231
> ----------------------------------------------------
> While the pull request does not actually resolve the error illustrated in this issue, it demonstrates how a user desired binary assignment operators for @property functions so much that (s)he attempted to implement them indirectly by modeling the value type as a struct and overloading its operators.  Had binary assignment operators been implemented the user probably would not have had any need expend such effort.
>
> Like the other users that commented in the issues above, I was a bit dismayed to discover that binary assignment operators were not implemented for @property functions.  When I submitted the pull request I was under the assumption that the only reason they weren’t implemented was because noone had gotten around to it yet.
>
> Walter has requested that I make this forum post to inform the community of this change in an effort to discuss its merits and/or shortcomings.
>
> We ask for your comments whether they be in approval or disapproval of this pull request so we can determine the best way forward.
>
> Thank you,
> Michael V. Franklin

merge it. but don't do unary inc/dec, as they are meant to be as atomic as possible.
August 15, 2017
Am Tue, 15 Aug 2017 03:53:44 +0000
schrieb Michael V. Franklin <none@none.com>:

> We ask for your comments whether they be in approval or disapproval of this pull request so we can determine the best way forward.
> 
> Thank you,
> Michael V. Franklin

+1. Then @property finally becomes useful ;-)


-- Johannes

August 15, 2017
On Tuesday, 15 August 2017 at 03:53:44 UTC, Michael V. Franklin wrote:
> An implementation of binary assignment operators for @property functions has been submitted to the DMD pull request queue at https://github.com/dlang/dmd/pull/7079.  It addresses the following issues:
>
> Issue 8006 - Implement proper in-place-modification for properties
> https://issues.dlang.org/show_bug.cgi?id=8006

I thought @property's behaviour had been removed from the language and even though the attribute remains, it doesn't actually do anything?


August 15, 2017
Am Tue, 15 Aug 2017 07:52:17 +0000
schrieb Gary Willoughby <dev@nomad.so>:

> On Tuesday, 15 August 2017 at 03:53:44 UTC, Michael V. Franklin wrote:
> > An implementation of binary assignment operators for @property functions has been submitted to the DMD pull request queue at https://github.com/dlang/dmd/pull/7079.  It addresses the following issues:
> >
> > Issue 8006 - Implement proper in-place-modification for
> > properties
> > https://issues.dlang.org/show_bug.cgi?id=8006
> 
> I thought @property's behaviour had been removed from the language and even though the attribute remains, it doesn't actually do anything?
> 
> 

You're probably thinking of the special optional/non-optional parenthesis rules and the -property compiler switch which was removed.

@property should still be used according to the style guide:
https://dlang.org/dstyle.html
and as far as I can tell it's heavily used in phobos.

Properties behave more like field variables in some traits: http://dlang.org/spec/traits.html https://dlang.org/phobos/std_traits.html

But I think that's the only relevant difference between properties and normal functions right now.

-- Johannes

August 15, 2017
On Tuesday, 15 August 2017 at 07:52:17 UTC, Gary Willoughby wrote:
> On Tuesday, 15 August 2017 at 03:53:44 UTC, Michael V. Franklin wrote:
>> An implementation of binary assignment operators for @property functions has been submitted to the DMD pull request queue at https://github.com/dlang/dmd/pull/7079.  It addresses the following issues:
>>
>> Issue 8006 - Implement proper in-place-modification for properties
>> https://issues.dlang.org/show_bug.cgi?id=8006
>
> I thought @property's behaviour had been removed from the language and even though the attribute remains, it doesn't actually do anything?

@property changes the typeof result (useful for introspection) [1][2]:

---
For the expression typeof(exp) where exp is an @property function, the type is the return type of the function, rather than the type of the function.
---

---
If the expression is a Property Function, typeof gives its return type.
---

[1] https://dlang.org/spec/declaration.html#typeof
[2] https://dlang.org/spec/function.html#property-functions
August 15, 2017
On Tuesday, 15 August 2017 at 03:53:44 UTC, Michael V. Franklin wrote:
> An implementation of binary assignment operators for @property functions has been submitted to the DMD pull request queue at https://github.com/dlang/dmd/pull/7079.  It addresses the following issues:
>
> Issue 8006 - Implement proper in-place-modification for properties
> https://issues.dlang.org/show_bug.cgi?id=8006
> ------------------------------------------------------------------
> This is the primary issue motivating the pull request.  The pull request is only a partial resolution of this issue; it implements binary assignment operators (e.g. `s.x += 2`), but not unary assignment operators (e.g. `s.x++`).  The plan is to implement unary assignment operators in a separate pull request after the fate of the binary assignment operators pull request is decided.

I'm a huge fan of this, but then I am of the mindset that @property functions should act like variables much in the way that they do for C# properties.


August 15, 2017
On Tuesday, 15 August 2017 at 03:53:44 UTC, Michael V. Franklin wrote:
> An implementation of binary assignment operators for @property functions has been submitted to the DMD pull request queue at https://github.com/dlang/dmd/pull/7079.
>
> [snip]
>
> Walter has requested that I make this forum post to inform the community of this change in an effort to discuss its merits and/or shortcomings.
>
> We ask for your comments whether they be in approval or disapproval of this pull request so we can determine the best way forward.

Thanks for the heads up. I think this would have been good for a DIP, to better guard against bad interplay with other D constructs.
I've added to the PRs a few comments that immediately came to mind.

It seems you already spent some time trying to break the new mechanics, that's good. But perhaps we need some more breakage ;-)  (for example I think we can dig a little deeper down the opBinary hole)

-Johan