coding-nim
Original:🇺🇸 English
Translated
Nim 2.x: macros, templates, compile-time, memory ARC/ORC, FFI, Nimble, systems programming
1installs
Added on
NPX Install
npx skill4agent add alphaonedev/openclaw-graph coding-nimTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →coding-nim
Purpose
This skill equips the AI to handle Nim 2.x programming tasks, focusing on advanced features like macros, templates, compile-time execution, memory management (ARC/ORC), FFI for interoperability, Nimble for package handling, and systems programming. Use it to generate, debug, and optimize Nim code efficiently.
When to Use
Apply this skill for systems-level programming needing low-level control, such as embedded systems, performance-critical apps, or when integrating with C/C++ via FFI. Use it for projects requiring compile-time metaprogramming (e.g., via macros) or automatic memory management with ARC/ORC to avoid manual garbage collection.
Key Capabilities
- Macros for code generation: Define reusable code transformations at compile-time.
- Templates for type-safe string-based metaprogramming.
- Compile-time execution: Run code during compilation using blocks.
static - Memory management: Switch between ARC (automatic reference counting) and ORC (optional reference counting) via compiler flags.
- FFI: Call external libraries (e.g., C functions) without wrappers.
- Nimble: Manage dependencies and build projects like a package manager.
- Systems programming: Direct hardware access, concurrency, and cross-platform compilation.
Usage Patterns
To accomplish tasks, structure Nim code with modules and use the compiler for builds. For macros, define them in a separate proc and invoke at compile-time. When writing FFI code, use the pragma for C functions. For memory management, specify or flags during compilation. Always test code with before full builds to catch errors early. Integrate templates for generic functions to reduce boilerplate.
importc--gc:arc--gc:orcnim checkCommon Commands/API
Use the Nim compiler () for core operations. Compile a file: (flags: for run, for minimal output). For Nimble, install packages: . Define a macro:
nimnim c --verbosity:0 -r main.nim-r--verbosity:0nimble install somepkgnim
macro doubleIt(x: expr): stmt =
result = quote do: `x` * 2Call it as . For FFI, import C: . Config format: Use for settings, e.g., to specify compiler. Environment variables: Set for package cache.
echo doubleIt(5)proc printf(format: cstring; args: varargs[pointer]) {.importc: "printf", header: "<stdio.h>".}nim.cfggcc.exe = "gcc"$NIMBLE_DIRIntegration Notes
Integrate Nim with other languages via FFI; for C++, use . To embed in a project, compile Nim code to a shared library: . For CI/CD, use GitHub Actions with: . If auth is needed (e.g., for Nimble registry), set env vars like for private repos. Link against external libs: Add for OpenSSL. Ensure path configurations match, e.g., add in .
importcppnim c --app:lib -d:release mylib.nimrun: nim c --opt:speed file.nim$NIMBLE_TOKEN--passL:-lsslpath = "/path/to/headers"nim.cfgError Handling
In Nim, use try-except blocks for runtime errors:
nim
try:
raise newException(ValueError, "Invalid input")
except ValueError:
echo "Handled error: ", getCurrentExceptionMsg()For compile-time errors, enable detailed output with . Check for memory issues by switching to ORC: . Parse compiler output for specifics; common flags include to catch undeclared vars. Log errors with or a logging library like .
nim c --verbosity:2 file.nimnim c --gc:orc file.nim--warnings:onechochroniclesConcrete Usage Examples
- Define and use a macro for code generation: To create a loop macro, write:
nim
macro forEach(items: seq, body: stmt): stmt =
result = quote do: for item in `items`: `body`Use it as: . This generates efficient loops at compile-time.
forEach(@[1, 2, 3], echo item)- Use FFI to call a C function: Import and call a C printf:
nim
proc cPrintf(format: cstring) {.importc: "printf", header: "<stdio.h>".}
cPrintf("%s\n", "Hello from Nim")Compile with to link C headers, enabling seamless integration.
nim c --passC:-I/usr/include file.nimGraph Relationships
- Related to cluster: coding
- Connected via tags: nim (direct link to language-specific skills), systems (links to low-level programming tools), coding (broad connections to other coding skills like coding-python or coding-c)