TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Introduction to Metaprogramming in Nim

101 点作者 vbit将近 9 年前

4 条评论

bluejekyll将近 9 年前
I like the macros definitions. Those are very elegant. I use macros in Rust, but I don&#x27;t find them very intuitive, or easy to get right. They are ridiculously powerful, but it feels like learning a new language, like there are three languages in Rust, safe, unsafe, and macros (don&#x27;t get me wrong I love Rust, and enjoy it the more I use it).<p>Out of curiosity, could someone who&#x27;s written in both languages, give their opinion on the differences between the two macro systems?
pathsjs将近 9 年前
Other examples of things one can do with metaprogramming: async&#x2F;await <a href="http:&#x2F;&#x2F;nim-lang.org&#x2F;docs&#x2F;asyncdispatch.html#async" rel="nofollow">http:&#x2F;&#x2F;nim-lang.org&#x2F;docs&#x2F;asyncdispatch.html#async</a> , pattern matching <a href="http:&#x2F;&#x2F;andreaferretti.github.io&#x2F;patty&#x2F;" rel="nofollow">http:&#x2F;&#x2F;andreaferretti.github.io&#x2F;patty&#x2F;</a> , for&#x2F;do comprehensions <a href="https:&#x2F;&#x2F;github.com&#x2F;vegansk&#x2F;nimfp&#x2F;blob&#x2F;master&#x2F;tests&#x2F;fp&#x2F;test_forcomp.nim" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;vegansk&#x2F;nimfp&#x2F;blob&#x2F;master&#x2F;tests&#x2F;fp&#x2F;test_f...</a>
评论 #11853092 未加载
jboy将近 9 年前
For anyone unfamiliar with Nim, I&#x27;ll give a quick summary of Nim generics, templates &amp; macros, in terms of C, C++, Java &amp; Lisp:<p>* Nim generics = parametrization-by-type of procs (functions) or other type definitions. Happens at compile time. Like Java generics or C++ templates, except that it uses [ ] rather than &lt; &gt;.<p>* Nim templates = direct textual substitution of code at the call-site at compile-time, like the C preprocessor, except that: 1. It operates upon a parsed Nim AST rather than plain-text code; 2. It&#x27;s (by default) hygenic; and 3. The syntax is the same as regular Nim language syntax (in contrast to the crippled C preprocessor syntax).<p>* Nim macros = compile-time evaluation of code to perform side-effects, one of which may be inserting new code at the call-site. Nim macros are most like Lisp macros. An invoked Nim macro receives a parsed Nim AST as a tree data-structure, and is able to traverse &amp; manipulate that AST, or create &amp; output a new AST. When evaluating macros, the Nim compiler runs the macro code in a compile-time Nim interpreter, so macros can invoke any other functions, allocate data-structures, etc. And again, the syntax is the same as regular Nim language syntax.<p>[There&#x27;s another Nim language feature that I really like, which I think is worth mentioning here: the `const` keyword, to define constants. Nim provides `var` to define variables that are read-write storage boxes, and `let` for single-assignment storage boxes. `const` is like `let`, except it&#x27;s evaluated at compile time. This means you can evaluate arbitrarily-complicated expressions (including function calls) at compile time, obtaining the result as a constant of the appropriate result type, which can then be inlined at all usage-sites -- just as if you&#x27;d entered the literal value directly in your code.]<p>With this background in place, I can finally get to my main point:<p>When I&#x27;m getting excited about Nim to friends, I tell them that I think Nim macros are the best tradeoff between an expressive Python-like syntax &amp; powerful AST-based, Lisp-like macros.<p>You see, no-one would dispute that it is very elegant to use regular function syntax to operate upon homoiconic code as a data-structure. And no-one would dispute that operating upon a pre-parsed AST is superior to crude text-concatenation (like in the C preprocessor). But as a commenter on HN pointed out in a recent thread about Lisp:<p>&quot;&quot;&quot;<i>So, the real question is why did such a magical language lose to the upstarts that all appeared in the late 80&#x27;s and early 90&#x27;s: Perl, Python, Tcl, Lua, etc. Answer: files, strings, and hash tables. All of those languages make mangling text pathetically easy. Perl is obviously the poster-child for one-liners, but all of those make that task pretty darn easy. Lisp makes those tasks really annoying. Just take a look at the Common Lisp Cookbook for strings: <a href="http:&#x2F;&#x2F;cl-cookbook.sourceforge.net&#x2F;strings.html" rel="nofollow">http:&#x2F;&#x2F;cl-cookbook.sourceforge.net&#x2F;strings.html</a> </i>&quot;&quot;&quot; -- <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=11700176" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=11700176</a><p>Dense language syntax is beneficial because it enables brevity for frequently-occurring operations. For example: string indexing, slicing &amp; especially regex matching, if you do a lot of text-processing; inline arithmetic operators if you do a lot of arithmetic; and array operators, if you do a lot of matrix processing.<p>Nim&#x27;s macros combine a dense (Python-like) language syntax with powerful AST-based macros, enabling you to traverse &amp; manipulate the AST just like any other data-structure.
justinlivi将近 9 年前
As someone who&#x27;s never coded in either go or nim, this seems to me to be the exact opposite ideology of golang. The metaprogramming is very cool, but I imagine sharing a code base that utilizes it heavily is a nightmare.
评论 #11851919 未加载
评论 #11851987 未加载