opUnaryAssign
Quirin Schroll
qs.il.paperinik at gmail.com
Thu Jul 4 11:11:27 UTC 2024
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.
More information about the dip.ideas
mailing list