Thread overview
opUnaryAssign
Jul 03
monkyyy
July 03

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).

July 03

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

July 04

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, clearly +obj has precedence over =. The precedence table is here. 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.