Foreach
A sugary for loop macro with syntax for typechecking loop variables
Install / Use
/learn @disruptek/ForeachREADME
foreach
A sugary for loop macro with syntax for typechecking loop variables. I've found that this syntax is as helpful for documentation as it is for eliminating errors.
Example
import foreach
let a = [1, 2, 3]
foreach n in a.items of int:
echo n, " is an int"
or
import json
import foreach
let j = %* {
"one": 1,
"two": "2",
}
foreach k, v in j.pairs of string and JsonNode:
echo k, " is a string"
echo v, " is a JsonNode"
but this will now fail at compile-time:
import json
import foreach
let j = %* {
"one": 1,
"two": "2",
}
# Error: loop variable `v` isn't a `string`
foreach k, v in j.pairs of string and string:
echo k, " is a string"
echo v, " is a string"
and you can use this to validate tuple field order/names:
import json
import foreach
type
JsonKeyValue = tuple[key: string; value: JsonNode]
let j = %* {
"one": 1,
"two": "2",
}
# Error: loop variable `pair` isn't a `JsonKeyValue`
foreach pair in j.pairs of JsonKeyValue:
assert pair.key is string
and for convenience, these compile to "normal" for loops:
import json
import foreach
let j = %* {
"one": 1,
"two": "2",
}
foreach k in 1 .. 5:
assert k > 0
foreach k, v in j.pairs:
echo k, " is a string"
echo v, " isn't really a string"
Related Skills
node-connect
338.0kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
83.4kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
338.0kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
83.4kCommit, push, and open a PR
