Operators over mutation forms :=
#296
-
Hi! Is it possible to write macros of the following form: macro
| '$obj += $vl': '$obj := $obj + $vl'
class Ball(mutable x,mutable y)
def ball: Ball(10,10)
ball.x += 2.0 The above fails with the error: // fails with left-hand argument is not a mutable identifier |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
That's an interesting example that is currently not possible. The problem is that the left-hand side of an infix expression operator must be parsed as an expression, because parsing it that way is necessary to find the infix operator afterward (by the enforest algorithm). But More generally, the issue is that the left-hand side of |
Beta Was this translation helpful? Give feedback.
-
Another idea: instead of trying to have l-value positions, have a notion of assignment operators. Then, forms like The protocol for assignment-operator expansion would be a little different from expression-operator expansion, although implementing one as a pattern-based macro would probably look about the same. The "fails with left-hand argument is not a mutable identifier" error message (at the start of this thread) could include the potential solution of defining an assignment operator. |
Beta Was this translation helpful? Give feedback.
I don't think that working at a lower level can help here, at least not in a general way. The fact that the left-hand side of
:=
isn't an expression is a more fundamental mismatch with theenforest
function, so there would have to be a change toenforest
or the way that it is used for expressions.Copying
:=
doesn't help because the handling of:=
inball.x := ...
is in the implementation of.
. Using the very latest Rhombus, you could change the way.
works with specificallyx
andy
for aBall
using thedot
class clause, but this doesn't feel like a very good solution: