Migrating to Rad v0.7¶
Version 0.7 introduces a breaking change to for-loop syntax. This guide will help you update your scripts.
Breaking Change: For-Loop Index Syntax¶
What Changed¶
The old implicit index syntax—where adding a second variable would make the first variable the index—has been removed. This syntax was confusing because adding a variable changed the meaning of existing variables.
Old Syntax (No Longer Works)¶
// This no longer works:
for idx, item in items:
print(idx, item)
// Also broken:
for i, name, age in zip(names, ages):
print(i, name, age)
New Syntax¶
Use the with <identifier> clause to access loop context:
// New way to access index:
for item in items with loop:
print(loop.idx, item)
// With zip:
for name, age in zip(names, ages) with loop:
print(loop.idx, name, age)
The context object provides:
loop.idx- Current iteration index (0-based)loop.src- Immutable snapshot of the original collection
Migration Steps¶
-
Find all for-loops with two or more variables
-
Determine if the first variable is an index
- If yes → use the
with loopsyntax -
If no (it's unpacking nested lists) → no change needed
-
Update the syntax
Example 1: Simple Index Access¶
// Before
for idx, name in names:
print(idx, name)
// After
for name in names with loop:
print(loop.idx, name)
Example 2: With zip¶
// Before
for idx, name, age in zip(names, ages):
print(idx, name, age)
// After
for name, age in zip(names, ages) with loop:
print(loop.idx, name, age)
Example 3: Unpacking (No Change Needed)¶
// This still works - unpacking nested lists
pairs = [["a", 1], ["b", 2]]
for letter, number in pairs:
print(letter, number)
// Can add context if you want the index too
for letter, number in pairs with loop:
print(loop.idx, letter, number)
Example 4: Map Iteration¶
// Before (if you had this pattern)
for idx, key in myMap:
print(idx, key)
// After
for key in myMap with loop:
print(loop.idx, key)
// Key-value iteration is unchanged
for key, value in myMap:
print(key, value)
// With context
for key, value in myMap with loop:
print(loop.idx, key, value)
Error Messages¶
If Rad detects you might be using the old syntax, it will show a helpful error:
Error: Cannot unpack 'int' into 2 values
Note: The for-loop syntax changed. It looks like you may be using the old syntax.
Old: for idx, item in items:
New: for item in items with loop:
print(loop.idx, item)
Why This Change?¶
The old syntax had several problems:
- Confusing: Adding a variable changed the meaning of the first variable
- Inconsistent: Didn't work well with
zip()and unpacking - Error-prone: Easy to accidentally shift variable meanings
The new syntax is explicit, consistent, and works the same way in all contexts.
Bonus: Rad Block Lambda Context¶
As part of this release, rad block map and filter lambdas can now accept an optional context parameter:
Population:
map fn(p, ctx) "{p / sum(ctx.src) * 100:.1}%"
The context provides:
ctx.idx- Current row indexctx.src- Full column data (immutable snapshot)ctx.field- Field name being transformed
See the Rad Blocks guide for details.