Gems

  1. DRY with a scoped template
  2. See through sugar with --expandArc

DRY with a scoped template

You have some code that repeatedly says s[^1], always referring to the last element of the same object. One way to do that more neatly is to have a template exactly where it's happening:

proc tedious(s: var seq[int]) =
  inc s[^1]
  inc s[^1]
  inc s[^1]
  echo s[^1]

proc slick(s: var seq[int]) =
  template last: var int = s[^1]
  inc last
  inc last
  inc last
  echo last

var s = @[1, 2, 3]
tedious s
slick s

See through sugar with --expandArc

ARC/ORC work by injecting lifetime functions into your code, so they came with a debugging tool to show what the resulting code looks like. That's useful in general, for example to confirm that the 'tedious' and 'slick' procedures in the last example are equivalent:

# nim --hints:off --expandArc:tedious --expandArc:slick r localtemplate.nim 
--expandArc: tedious

var :tmpD
try:
  inc s[1], 1
  inc s[1], 1
  inc s[1], 1
  echo [
    :tmpD = `$`(s[1])
    :tmpD]
finally:
  `=destroy`(:tmpD)
-- end of expandArc ------------------------
--expandArc: slick

var :tmpD
try:
  template last(): var int =
    s[^1]

  inc s[1], 1
  inc s[1], 1
  inc s[1], 1
  echo [
    :tmpD = `$`(s[1])
    :tmpD]
finally:
  `=destroy`(:tmpD)
-- end of expandArc ------------------------
6
9
@[1, 2, 9]

Toplevel code you can expand with --expandArc:module_name:

# nim --hints:off --expandArc:localtemplate r localtemplate.nim
--expandArc: localtemplate

var
  s
  :tmpD
try:
  s = @[1, 2, 3]
  tedious s
  slick s
  echo [
    :tmpD = `$`(s)
    :tmpD]
finally:
  `=destroy`(:tmpD)
-- end of expandArc ------------------------
--expandArc: localtemplate

-- end of expandArc ------------------------
6
9
@[1, 2, 9]