assigns/impl

Source   Edit  

The assign macro in this module is overloadable, and the openAssign template creates a NimNode that calls a forced open symbol of assign with the AST of the left hand side, AST of the right hand side, and the flag of whether or not it is a let, var, or mutating assignment. You can use the implementAssign and implementAssignExported templates as a shorthand for declaring these overloads.

Types

AssignBoundError = object of AssignError
error for failed bound checks in assignments Source   Edit  
AssignContainsError = object of AssignError
error for failed contains checks in assignments Source   Edit  
AssignEqualityError = object of AssignError
error for failed equality checks in assignments Source   Edit  
AssignError = object of CatchableError
any kind of check error in assignments Source   Edit  
AssignKind = enum
  akLet, akVar, akSet
Propagated flag of how definitions should create assignments. Source   Edit  
AssignOptionError = object of AssignError
error for failed contains checks in assignments Source   Edit  

Procs

proc defaultAssign(lhs, rhs: NimNode; kind = akLet): NimNode {....raises: [],
    tags: [], forbids: [].}
The default interpretation of a definition. Source   Edit  

Macros

macro assign[T](lhs; rhs: Option[T]; kind: static AssignKind = akLet): untyped
The library's builtin overload of assign for Option[T]. Source   Edit  
macro assign[T](lhs; rhs: T; kind: static AssignKind): untyped
Handles definitions for a type T. Can be overloaded, optionally via the convenience macros implementAssign and implementAssignExported. Source   Edit  

Templates

template assignCheckContains(a, b): untyped
template for equality checks in assignments Source   Edit  
template assignCheckDefaultFail(body)
default behavior for assignment check failing with expression body, which is just to run body Source   Edit  
template assignCheckEqual(a, b): untyped
template for equality checks in assignments Source   Edit  
template assignCheckFail(body)
if assignCheckBreakpoint is declared, calls it, otherwise falls back to body Source   Edit  
template assignCheckGreater(a, b): untyped
template for non-equality checks in assignments Source   Edit  
template assignCheckGreaterEqual(a, b): untyped
template for non-equality checks in assignments Source   Edit  
template assignCheckLess(a, b): untyped
template for non-equality checks in assignments Source   Edit  
template assignCheckLessEqual(a, b): untyped
template for non-equality checks in assignments Source   Edit  
template assignCheckNotContains(a, b): untyped
template for non-equality checks in assignments Source   Edit  
template assignCheckNotEqual(a, b): untyped
template for non-equality checks in assignments Source   Edit  
template assignCheckNotType(a, b): untyped
template for non-type checks in assignments Source   Edit  
template assignCheckOption(a): untyped
template for equality checks in assignments Source   Edit  
template assignCheckType(a, b): untyped
template for type checks in assignments Source   Edit  
template implementAssign(T; body) {.dirty.}
Implements an overload (non-exported) of assign for T with the macro body being body. Macro argument names in order are lhs, rhs and kind. Has shorthand aliases open and default for openAssign and defaultAssign respectively.

Example:

import std/macros
type LinkedList[T] {.acyclic.} = ref object
  leaf: T
  next: LinkedList[T]

implementAssign LinkedList:
  let newLhs = if lhs.kind == nnkBracket and lhs.len == 1: lhs[0] else: lhs
  if newLhs.kind == nnkInfix and newLhs[0].eqIdent"|":
    newStmtList(
      open(newLhs[1], newDotExpr(rhs, ident"leaf")),
      open(newLhs[2], newDotExpr(rhs, ident"next")))
  else:
    default()

let a = LinkedList[int](leaf: 1, next:
  LinkedList[int](leaf: 2, next:
    LinkedList[int](leaf: 3, next: nil)))

import assigns/syntax
x | [y | [z | _]] := a
doAssert (x, y, z) == (1, 2, 3)
Source   Edit  
template implementAssignExported(T; body) {.dirty.}
Same as implementAssign but exports the generated macro. Source   Edit  
template openAssign(lhs, rhs: NimNode; ak: AssignKind = akLet): NimNode
Creates a node that calls an open symbol assign with lhs and rhs. Source   Edit