News for hackers. Stuff that matters.
Re:Nim Programming Language v0.16.0 released by mrkgnao on Monday January 09, 2017 @10:44AM (#13357104)
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?

Re:Nim Programming Language v0.16.0 released by Tarean on Tuesday January 10, 2017 @08:37AM (#13364626)
There 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.

Re:Nim Programming Language v0.16.0 released by evacchi on Monday January 09, 2017 @12:27PM (#13357951)
you 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
Re:Nim Programming Language v0.16.0 released by arc776 on Monday January 09, 2017 @10:48AM (#13357152)
Macros & templates are AST based.