alan icon indicating copy to clipboard operation
alan copied to clipboard

Re-assignment statements

Open dfellis opened this issue 8 months ago • 0 comments

Still not yet implemented is the foo = bar; re-assignment of a variable or property of the variable to a new value.

The first could be a pretty obvious translation back into Rust and Javascript, but the latter would require some rewriting of the left-hand side, and re-using the set function concept could do a lot here.

The issue is if you use set, then you are allowing extensibility of the setting syntax, which is great, but then you aren't allowing it for the basic value reassignment.

Unless if you do, where you allow a set with only two arguments, so let foo = true; foo.set(false); would work, effectively making all variables object-like, which is really cool but doesn't work with intrinsic and bound types in the foundational languages.

Maybe that's just something to deal with and have automatically-generated set "functions" for all of these types that just turns into the assignment statement for them, but user-defined types can be fancier.

This does bring up another point, though: setting a value could be fallible. With the .set call that's more obvious, and there could be some logic in the compiler that all returned values must be consumed, but what about assignment statements? Should the equal sign become just another operator except for the let var = ... and const var = ... contexts?

That could do it. Making sure this isn't annoying or weird would depend on most of the set functions returning void, though, which breaks method chaining, so setting multiple records of a HashMap, for instance, could be annoying then.

But then there's another problem: the LHS meaning of [val] is different from the RHS meaning. On the left of the = we want to set that sub-value, while on the right we want to get it. In C, where the idiom comes from, those are basically the same, since we're just messing with memory addresses and the values housed in them, but for Alan (and Javascript) they aren't.

So we probably do still need a separate assignment statement so it can be transformed appropriately, but we need to make it part of the "BaseAssignment" set, so we can get the value returned by the assignment if it's set function doesn't return void.

dfellis avatar Jun 11 '24 00:06 dfellis