Last active
November 7, 2019 01:19
-
-
Save rayman22201/1cfb980293a0a55091a4885f9d39aa5e to your computer and use it in GitHub Desktop.
explaining cross module templates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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