Copying a series

Red tries to reuse series and objects in memory as much as possible, for performance reasons. This means that using variables to point to the same series is often a bad idea and is the cause of hard-to-find bugs in your code. We have already told you in the Assigning and copying section in Chapter 3, Using Words, Values, and Types, that using copy is the safest way to ensure that your variables point to different memory locations. That way, changes to one variable don't affect the other.

To appreciate this fact, examine and try to predict the output of the following code:

;-- see Chapter05/copying.red:not-expected: [    var1: ""    append var1 "*"    print var1]loop 3 [do not-expected]

The do word executes the not-expected code block ...

Get Learn Red - Fundamentals of Red now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.