Skip to content

Instantly share code, notes, and snippets.

@rayman22201
Last active November 7, 2019 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rayman22201/1cfb980293a0a55091a4885f9d39aa5e to your computer and use it in GitHub Desktop.
Save rayman22201/1cfb980293a0a55091a4885f9d39aa5e to your computer and use it in GitHub Desktop.
explaining cross module templates
import templates
let myThing = someProcThatReturnsOption()
printFileBool(myThing, "string") # calls the template
# this essentially expands to:
# it's a fancy copy / paste, but creates a new scope. This is what is known in the lisp world as "hygenic".
# A template will not import other things into your module automatigically.
block:
if not myThing.isNone: # it's just a copy / paste. Nim will not automagically "import Option" for you.
echo "I'm not None!"
import Option # Note: This will not get imported into any other modules.
template printFileBool*(token: untyped, tname: string) =
if not token.isNone:
echo "I'm not None!"
# You could do this:
template ImportAndPrintFileBool*(token: untyped, tname: string) =
import Option # this line will get copy / pasted into the module, and you will have the proc available there.
# It's hacky though. You will import every time you call the template, which is silly.
if not token.isNone:
echo "I'm not None!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment