Hi all,
I'm just wondering if anyone can explain / clarify / elaborate upon the difference between static[T] (for example, static[int] or static[string]: http://nim-lang.org/manual.html#static-t ) and the T{lit} parameter constraint (for example, int{lit} or string{lit}: http://nim-lang.org/manual.html#parameter-constraints , http://nim-lang.org/manual.html#typedesc )?
So far, my best understanding is this:
Is this understanding correct? Is there any more to it than this?
Thanks for any clarification.
Is there any more to it than this?
T{lit} can be used for procs, e.g. for overloading:
proc p(n: int{lit}): int = n*10
proc p(n: int): int = n*100
echo 5.p # 50, 1st proc called
let five = 5
echo five.p # 500, 2nd proc called
In tests int{lit} matches also consts (const five = 5) and compile-time computed expressions (like 2 + 3), though manual says {lit} is only for literals (like 5). I don't know which behavior is intended.
@LeuGim, thanks for that clarification that T{lit} can also be used for procs! And the int{lit} vs int proc parameter overloading effect is very interesting -- I hadn't encountered that before.
@Araq, thanks for the clarification on the theoretical differences. The intention of differentiating syntactic vs semantic matching is an intriguing distinction. Now that I understand the intention of syntactic matching, that language feature appeals to me.
I think I also understand what is intended by semantic matching using static[T], but it's more subtle. And as a beginner with the language, I agree that it would be a little easier on beginners if those two different concepts (syntactic vs semantic matching) could be unified somehow into a single language feature. :)
In the meantime, I'll read a bit about the Nim compiler architecture: http://nim-lang.org/intern.html#the-compiler-s-architecture
By the way, I just skimmed that "Compiler Internals" page, and I saw a mention of a non-existent ast module: http://nim-lang.org/intern.html#the-syntax-tree
I assume that should be updated to point to the macros module?
jboy: I saw a mention of a non-existent ast module
The ast module is part of the compiler, not the standard libs.