News for hackers. Stuff that matters.
Re:Building a simple room-based chat application in Nim (usi... by nyanpasu64 on Saturday December 18, 2021 @06:10AM (#29603594)
There's some odd syntax in this post, which passes colon-delimited indented code blocks to function calls, than control flow primitives. It reminds me of Ruby/Kotlin blocks or Swift's blocks (criticized in https://blog.metaobject.com/2020/06/the-curious-case-of-swif...), but it's quite unusual from my Python/C++/Rust background and I think it's worth explaining in the blog post, since I don't know if it's passing a key-value map/struct or a lambda.
Re:Building a simple room-based chat application in Nim (usi... by planetis on Saturday December 18, 2021 @07:24AM (#29603966)
`buildHtml` is a macro, the code passed to it via it's invocation (the 'colon-delimited indented code blocks') is then transformed as explained in the readme https://github.com/karaxnim/karax#hello-world and compiled. Macros are explained in the manual https://nim-lang.github.io/Nim/manual.html#macros
Re:Building a simple room-based chat application in Nim (usi... by ajusa on Saturday December 18, 2021 @01:28PM (#29607058)
+1 to what planetis mentioned. I didn't include a full explanation of the macro because I was more or less showing off a project - not doing a full tutorial of all of the internal bits and implementation details of what libraries I was using.

So in this case, the `template` bits are doing plain code substitution at compile time, and the buildHtml `macro` is doing more advanced code generation, also at compile time. This means that it should avoid the overhead you mentioned with a key-value struct/lambda.

For what it's worth, this could also have been implemented as a `proc` which would then take in a lambda/anonymous function to generate the HTML, but that incurs additional overhead and requires a bit of extra syntax, which felt like a hassle.