Nim

From G1Joshi/Agent-Skills · View interactive version

Nim is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert Nim systems programming assistance covering Python-like syntax, C/C++ compilation, macros, and ARC memory management. Use when writing high-performance CLI tools, games, or systems software.

How to use this skill

Skill file

Nim

Nim is a statically typed systems programming language offering Python-like expressive syntax, compile-time metaprogramming, and deterministic ARC/ORC memory management compiling to C, C++, and JavaScript.

When to Use

Quick Start

type
  Person = object
    name: string
    age: int

proc greet(p: Person) =
  echo "Hello, ", p.name, "! You are ", p.age, " years old."

let alice = Person(name: "Alice", age: 30)
greet(alice)

Core Concepts

Pythonic Indentation with C-Level Performance

Clean, readable syntax that compiles directly into optimized C/C++ code:

type
  Customer = object
    id: int
    name: string
    active: bool

proc calculateBonus(customer: Customer, spend: float): float =
  if customer.active:
    result = spend * 0.05
  else:
    result = 0.0

let vip = Customer(id: 415, name: "Alice", active: true)
echo "Bonus earned: $", calculateBonus(vip, 1500.0)

ARC / ORC Deterministic Memory Management

Automatic Reference Counting with cycle detection (ORC) eliminates stop-the-world garbage collection pauses:

# Compile with modern deterministic ORC memory management
nim c --mm:orc -d:release main.nim

Powerful Compile-Time Macro System

Manipulates the Abstract Syntax Tree (AST) directly during compilation:

import macros

macro debugLog(expr: untyped): untyped =
  let strExpr = expr.toStrLit
  quote do:
    echo "DEBUG: ", `strExpr`, " = ", `expr`

let x = 42
debugLog(x * 2) # Prints: DEBUG: x * 2 = 84

Common Patterns

Compile-Time Macro Code Generation

Problem: Repetitive boilerplate for serializing or validating domain objects.

Solution:

Use Nim's AST macros executed at compile time:

import macros

macro printVarNameAndValue*(v: untyped): untyped =
  result = newStmtList()
  result.add(newCall("echo", newLit(v.repr & " = "), v))

let score = 98
printVarNameAndValue(score) # Outputs: score = 98

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :--------------------------------------------------- | :-------------------------------------------------------- | :------------------------------------------------------------------------------- |

| Error: type mismatch: got <...> but expected <...> | Strict static type mismatch. | Check types and use conversion procedures like int(), float(), or $. |

| Error: undeclared identifier | Symbol referenced before declaration or missing import. | Ensure identifier is declared earlier or exported with * from imported module. |

| SIGSEGV in C backend execution | Unsafe pointer dereference or C interop memory violation. | Compile with --mm:orc and enable --debugger:native for debug traces. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.