# Introduction to Metaprogramming in Nim

DevFeed: [Introduction to Metaprogramming in Nim](<https://devfeed.tech/articles/introduction-to-metaprogramming-in-nim-30821.md>)

Original publisher: [Read original article](<https://hookrace.net/blog/introduction-to-metaprogramming-in-nim/>)

Published: 2016-06-05T22:00:00Z

Content type: tutorial

Language: en

Sources: [Dennis Felsing](<https://devfeed.tech/sources/dennis-felsing.md>)

Topics: [Nim](<https://devfeed.tech/topics/nim.md>), [Programming](<https://devfeed.tech/topics/programming.md>), [generics](<https://devfeed.tech/topics/generics.md>)

Tags: [article](<https://devfeed.tech/tags/article.md>), [generics](<https://devfeed.tech/tags/generics.md>), [metaprogramming](<https://devfeed.tech/tags/metaprogramming.md>), [nim](<https://devfeed.tech/tags/nim.md>), [programming](<https://devfeed.tech/tags/programming.md>)

## AI overview

This tutorial introduces metaprogramming in Nim and explains a progression from ordinary procedures and iterators to generic procedures, closure iterators, templates, and macros. It describes how these constructs can work with or transform program code.

## Source excerpt

Introduction to the Introduction (Meta-Introduction) Wikipedia gives us a nice description of metaprogramming: Metaprogramming is the writing of computer programs with the ability to treat programs as their data. It means that a program could be designed to read, generate, analyse and/or transform other programs, and even modify itself while running. In this article we will explore Nim's metaprogramming capabilities, which are quite powerful and yet still easy to use. After all great metaprogramming is one of Nim's main features. The general rule is to use the least powerful construct that is still powerful enough to solve a problem, in this order: Normal procs and inline iterators Generic procs and closure iterators Templates Macros So before looking at Nim's two main metaprogramming constructs, templates and macros, we'll look at what we can do with procs and iterators as well. Regular Programming Constructs Normal procs We're in normal programming land here. Regular procedures are what you know as functions elsewhere and they're pretty easy to define and use: proc sayHi(name: string) = echo "Hello ", name sayHi("World") sayHi "World" "World".sayHi Generic procs With generics we can define procs that work on multiple types. Actually a new proc will be generated based on our generic definition for each instantiation: proc min[T](x, y: T): T = if x < y: x else: y echo min(2, 3) # more explicitly: min[int](2, 3) echo min("foo", "bar") # min[string]("foo", "bar") Inline iterators Inline iterators are the default iterators in Nim. They get compiled into high performance loops: iterator reverseItems(x: string): char = for i in countdown(x.high, x.low): yield x[i] for c in "foo".reverseItems: echo c So this code gets compiled into: let x = "foo" for c in countdown(x.high, x.low): let c = x[i] echo c Of course we can make iterators generic too: iterator reverseItems[T](x: T): auto = for i in countdown(x.high, x.low): yield x[i] Closure iterators Inline iterators simultane