4 ms·
Macros implemented with ... string interpolation? This looks like a suboptimal idea. Edit: I spoke too soon. This looks like code generation, and I guess that
by mrkgnao 10y ago
Macros implemented with ... string interpolation? This looks like a suboptimal idea.
Edit: I spoke too soon. This looks like code generation, and I guess that you ultimately have to do stringy things at some point. But couldn't there be some kind of type-safe mechanism for this?
- arc776 10y agoMacros & templates are AST based.
- evacchi 10y agoyou can do both; you can opt-out of the type-safe mechanism and decide to just emit arbitrary code; it's a just one more tool in your belt
- Tarean 10y agoThere are basically three ways to do meta programming in nim. Simple replacement of tokens: const debug = true template log(msg: string) = if debug: stdout.writeLine(msg) You could also use other concrete types, generics and type checked or unchecked expressions instead of string. Macros are syntax tree transformations, similar to rust compiler plugins: import macros, strutils macro readCfgAndBuildSource(cfgFilename: string): typed = let inputString = slurp(cfgFilename.strVal) result = newStmtList() for line in inputString.splitLines: # Ignore empty lines if line.len < 1: continue var chunks = line.split(',') if chunks.len != 2: error("Input needs comma split values, got: " & line) let entry = quote do: const cfg`chunks[0]` = "`chunks[1]`" result.add(entry) if result.len < 1: error("Input file empty!") readCfgAndBuildSource("data.cfg") This reads a file and creates constants from it at compile time. Emit is a tad more complex. Nim is compiled to another language like c, c++ or llvm. Emit lets you emit code in the host language, mostly to wrap libraries. Don't think there is any way to make that typesafe.