Replies: 1 comment 1 reply
-
Here is my current thinking, it's early so I'll likely change my mind over the course of discussion. How templates should workNB:
Quick VersionIn their simplest form template declarations are a "function" of the form: More Details/Long VersionEach step is basically partial application, the two contexts are likely the most alien parts, so I'll describe what they're for first. When we give it a
When we give it a
Since this is all partial application the actual resolution of the identifiers in the partially applied function can be deferred to the callsite. As an optimisation we can partially evaluate the definition resolving all the immediately known bits and leaving the rest for the If you're curious how we get a Issues/Solution SketchThe problem with all this is that macros are horrible destructive little monsters, that can mess stuff up. :( For this I think the best approach would be that the AST emitted by the template call is representative of the "activation record" of the template. Meaning something like: # given mymodule.nim
template foo(a: int) =
a * 2
var a = 11
foo(3)
#[ expands to (mix of tree and syntax, hope it's clear enough):
stmtList:
let genSym"foo" {.definingCtx: "mymodule.foo".} # used by sym resolution
let a {.templateParam.} = IntLit(3) # registers in the `definingCtx`
infixCall
ident"*"
ident"a"
intLit 2
] In the expansion the first line within the Scoping the Finally, resolved symbols storing a ref to the Thoughts
|
Beta Was this translation helpful? Give feedback.
-
Recently in chat a discussion started about how to handle single pass symbol
resolution for templates. @zerbina and I discussed some preliminary thoughts
and we both seem to be leaning towards making use of a new symbol kind to
track a binding scope.
During the discussion I realized my own thinking was fuzzy, so I collected my
thoughts a bit and recorded them (see below). Hoping to use this thread to
memorialize some of the ideas and direction.
Beta Was this translation helpful? Give feedback.
All reactions