skinsuit/expand

Source   Edit  

expand macro, adds alias templates for every field of an object, like the macro from the library `with` but doesn't use a block, can replicate by just wrapping its use in block

Example:

import skinsuit/expand
type Foo = ref object
  first: int
  second: string
  third: float

proc useFoo(foo: Foo) =
  expand(foo)
  doAssert first == foo.first
  doAssert second == foo.second
  doAssert third == foo.third
  if true:
    third = float(first)
    second = "abc"
  doAssert float(first) == third
  doAssert second == "abc"

var foo = Foo(first: 1, second: "two", third: 3.0)
useFoo(foo)

Types

FieldAccessorKind = enum
  UntypedGetter, Getter, VarGetter, Setter
Source   Edit  
FieldAliasKind = enum
  TemplateAlias, Let, LetVarView
Source   Edit  

Consts

skinsuitExpandUseViews {.booldefine.} = false
whether or not to use experimental views when using variables and not templates Source   Edit  

Macros

macro expand(args: varargs[typed]): untyped
expandAs that allows multiple arguments, defaults to template alias Source   Edit  
macro expandAs(a: typed; kind: static FieldAliasKind): untyped

unwraps fields of an object to the current scope

kind decides if to use untyped templates or to unwrap to let variables etc

Source   Edit  
macro expandField(root: typedesc; field: untyped;
                  accessors: static set[FieldAccessorKind] = {UntypedGetter}): untyped

generates getters for fields of a field of an object to the original object

templ decides if to use untyped templates or to unwrap to let variables

Source   Edit