Nim api design

From planetis-m/skills · View interactive version

Nim api design is an AI agent skill from the GitHub repository planetis-m/skills. Design clear public Nim APIs for libraries and modules, including exported types, constructors, parameter ownership, lookup functions, error contracts, and container-style interfaces. Use when creating or reviewing a Nim library API, designing an exported module surface, or deciding how callers shou

How to use this skill

Skill file

Nim API Design

Rules

Public shape

lifetime is part of the contract.

forms are part of the contract.

for ordinary APIs.

items/mitems, pairs/mpairs, add, del, clear, incl/excl, and push/pop.

callers may safely mutate yielded values.

Contracts

(int for Natural or Positive).

protocol code spaces; use strings for open-ended or externally extensible token namespaces.

explicit strings (dirNorth = "north"); $ and parseEnum[T] always round-trip.

when they must match.

Constructors and conversions

the main path.

Sequence parameters

traversal or var openArray[T] for fixed-length element mutation.

instead of openArray[T].

Parameter ownership

when the proc takes ownership, and lent T only for borrowed returns.

auto-sink moves locals, their fields, tuple fields and indices, and direct-indexed seq/array

elements with no copy.

loop-indexed seq/array elements (arr[i]), and values reused later in the same scope.

Template parameters

expressions, names, types, or statement blocks the caller must provide—not like proc parameters.

Lookup surface

{.noinline, noreturn.} helper.

Borrowed and mutable access

updates, expose mutation procs instead.

Public boundary

accessors; an exported field would bypass them.

Workflow

  1. Choose the representation.

Start with a plain object. Switch to ref object only if the contract needs identity or aliasing.

  1. Name the public types.

Use named objects for semantic results and distinct types for domain identities.

  1. Name the constructor surface.

Use initX, newX, and toX in the stdlib style.

  1. Design the lookup surface.

Provide one strict path for required data and one explicit safe path for optional data.

  1. Choose parameter modes and borrowed access.

Use T when the caller's variable stays unchanged, var T when the proc changes it, sink T

when the proc takes ownership, and lent T to return a borrow. Use mutation procs when changes

need validation or related updates.

  1. Verify the contract.

Compile public examples. Exercise each mutation, ownership, and failure path.

Common Mistakes

| Mistake | Why it is wrong |

|---------|------------------|

| Starting with ref object for plain data | It adds aliasing and shared mutation where the API does not need them |

| Defaulting to methods or runtime dispatch | It hides behavior behind runtime polymorphism when a proc surface is simpler and clearer |

| Weakening a constrained public parameter to int and re-checking manually | It throws away a stronger boundary contract |

| Returning a silent default for required data | It hides missing-data bugs |

| Returning var T for controlled state | Callers can bypass validation and related updates |

| Returning a lent or var result through a temp local | The temp dies at return; the compiler rejects the dangling borrow |

| Using lent T for an input parameter | lent T is a borrowed return type; the compiler rejects it in parameter position |

| Assuming a sink call always moves the caller's variable | Nim copies the argument when it cannot prove last use |

| Wrapping a routine sink argument in ensureMove | Sink already performs last-use analysis; use ensureMove only when the code must fail instead of copy |

| Defining !=, >, or >= for a type | These override Nim's derived comparison templates and can make comparisons inconsistent |

| Assuming variant of branches scope their own fields | Reusing a name is a compile error |

References

initX and newX construction.

through per-branch payload objects.

and controlled-mutation paths.

parameters, and named results in one public API.

evaluation that a proc cannot express.

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.