www.digitalmars.com         C & C++   DMDScript  

digitalmars.dip.ideas - opUnaryAssign

reply Quirin Schroll <qs.il.paperinik gmail.com> writes:
Index operators (e.g. `obj[i]`) have special overloads so that 
they hook assignment: `obj[i] = rhs` lowers to 
`obj.opIndexAssign(rhs, i)`. Why not back-port this to 
dereferencing? `*obj` lowers to `obj.opUnary!"*"`, which, to be 
assignable, must return by reference. Why not add 
`opUnaryAssign(string op)` which can hook, in principle, `+obj = 
rhs`, `-obj = rhs`, `~obj = rhs`, `*obj = rhs`, `++obj = rhs`, 
and `--obj = rhs` (of which I expect only `*obj = rhs` to be used 
regularly).
Jul 03
parent reply monkyyy <crazymonkyyy gmail.com> writes:
On Wednesday, 3 July 2024 at 10:43:14 UTC, Quirin Schroll wrote:
 Index operators (e.g. `obj[i]`) have special overloads so that 
 they hook assignment: `obj[i] = rhs` lowers to 
 `obj.opIndexAssign(rhs, i)`. Why not back-port this to 
 dereferencing? `*obj` lowers to `obj.opUnary!"*"`, which, to be 
 assignable, must return by reference. Why not add 
 `opUnaryAssign(string op)` which can hook, in principle, `+obj 
 = rhs`, `-obj = rhs`, `~obj = rhs`, `*obj = rhs`, `++obj = 
 rhs`, and `--obj = rhs` (of which I expect only `*obj = rhs` to 
 be used regularly).
wouldnt `+obj=rhs` combined with "assignment returns" be ambiguous in some case? ~~idk why people use assignment returns, its incredibly backwards of sanity and im fuzzy on the syntax~~
Jul 03
parent Quirin Schroll <qs.il.paperinik gmail.com> writes:
On Wednesday, 3 July 2024 at 13:26:35 UTC, monkyyy wrote:
 On Wednesday, 3 July 2024 at 10:43:14 UTC, Quirin Schroll wrote:
 Index operators (e.g. `obj[i]`) have special overloads so that 
 they hook assignment: `obj[i] = rhs` lowers to 
 `obj.opIndexAssign(rhs, i)`. Why not back-port this to 
 dereferencing? `*obj` lowers to `obj.opUnary!"*"`, which, to 
 be assignable, must return by reference. Why not add 
 `opUnaryAssign(string op)` which can hook, in principle, `+obj 
 = rhs`, `-obj = rhs`, `~obj = rhs`, `*obj = rhs`, `++obj = 
 rhs`, and `--obj = rhs` (of which I expect only `*obj = rhs` 
 to be used regularly).
wouldnt `+obj=rhs` combined with "assignment returns" be ambiguous in some case?
In the [expression grammar](https://dlang.org/spec/expression.html), clearly `+obj` has precedence over `=`. The precedence table is [here](https://wiki.dlang.org/Operator_precedence). The gist is: 1. Postfix operators (includes call `()`, index `[]`, and member access `.`) 2. Prefix operators 3. Binary operators There are exceptions, e.g. binary `!` for templates is top, `^^` is stronger than unary prefix, and `=>` has different precedence on the left and right, but for the most part, the 3-item list is accurate.
Jul 04