I think the VarOrRef explanation in the book is a bit confusing. I think this explanation is much more concise and clear and could be added as an overview:
Every Expression node in the AST is resolved as a VarOrRef. An enum that represents either a variable, or a reference to a variable. The sole reason to use a reference is when the variable is mutable, in which case you must be able to go to the list of variables present in the scope and mutate the correct one (so that if some logic tries to mutate it, it can). That's why, a var_name is stored in a reference. We also pass a (start, len) tuple to handle mutable slices. As we need to remember exactly where we are in the original array. As a slice is a narrowing of an array, we must not lose track of which array we were looking at originally (as this is what needs to be mutated).
I think the
VarOrRef
explanation in the book is a bit confusing. I think this explanation is much more concise and clear and could be added as an overview:Every
Expression
node in the AST is resolved as aVarOrRef
. An enum that represents either a variable, or a reference to a variable. The sole reason to use a reference is when the variable is mutable, in which case you must be able to go to the list of variables present in the scope and mutate the correct one (so that if some logic tries to mutate it, it can). That's why, avar_name
is stored in a reference. We also pass a(start, len)
tuple to handle mutable slices. As we need to remember exactly where we are in the original array. As a slice is a narrowing of an array, we must not lose track of which array we were looking at originally (as this is what needs to be mutated).