Re: Please tell me about jester's "tmpl" files.
@mashingan Thank you for your reply! I read documents of "source-code filter" and I understood ! Thank you! And I understood that I can't re-render without re-compile in jester!
Re: Nim-lang is added as supported language in repl.it!
Wooot! Unfortunately, my organization-provided laptop runs Chrome OS, so this is great news for me
Re: gintro demo with two columns in a listview / gtktreeview and sortable
OK, as promised here is a very basic example of a three column treeview: import gintro/[glib, gobject, gtk] import gintro/gio except ListStore const GroceryList = [(true, 1, "Paper Towels"), (true, 2, "Bread"), (false, 1, "Butter"), (true, 1, "Milk"), (false, 3, "Chips"), (true, 4, "Soda")] ## Add three columns to the GtkTreeView. All three of the columns will be ## displayed as text, although one is a gboolean value and another is an integer. proc setupTreeView(treeview: TreeView) = var renderer: CellRendererText var column: TreeViewColumn ## Create a new GtkCellRendererText, add it to the tree view column and ## append the column to the tree view. for i, t in ["Buy", "Count", "Product"]: renderer = newCellRendererText() column = newTreeViewColumn() column.title = t column.packStart(renderer, true) column.addAttribute(renderer, "text", i) discard treeview.appendColumn(column) proc appActivate(app: Application) = let window = newApplicationWindow(app) window.title = "Grocery List" window.borderWidth = 10 window.position = WindowPosition.center window.setSizeRequest(250, 175) let treeview = newTreeView() setupTreeView(treeview) ## Create a new tree model with three columns, as string, gint and guint. ## store = gtk_list_store_new(, G_TYPE_BOOLEAN, G_TYPE_INT, G_TYPE_STRING) var h = [typeFromName("gboolean"), typeFromName("gint"), typeFromName("gchararray")] let store = newListStore(3, cast[pointer]( unsafeaddr h)) # cast due to bug in gtk.nim ## Add all of the products to the GtkListStore. for el in GroceryList: var val: Value var iter: TreeIter var gtype: GType let buy = el[0] let quantity = el[1] let product = el[2] store.append(iter) gtype = typeFromName("gboolean") discard gValueInit(val, gtype) setBoolean(val, buy) store.setValue(iter, 0, val) gtype = typeFromName("gint") val.unset discard gValueInit(val, gtype) setint(val, quantity) store.setValue(iter, 1, val) gtype = typeFromName("gchararray") val.unset discard gValueInit(val, gtype) setString(val, product) store.setValue(iter, 2, val) let scrolled_win = newScrolledWindow() scrolled_win.setPolicy(PolicyType.automatic, PolicyType.automatic) scrolled_win.add(treeview) window.add(scrolled_win) treeview.setModel(store) showAll(window) proc main = let app = newApplication("org.gtk.example") connect(app, "activate", appActivate) discard run(app) main() Run I took an old C example from the old GTK2 book of Andrew Krause, applied c2nim, mixed it with the other listview example, and fixed the rest. The ugly cast is still present, I have not touched gintro package yet, sorry. Of course this example does only display the three columns, there is no code for modifying it yet. But adding that should be not that hard, as seen from the first example. I noticed that you asked AT THE SAME Time at stackoverflow. Some consider that a bad habit, maybe you can at least link that one to this forum thread.
Re: Is it possible to add a proc after a proc in a macro?
What exactly do you want to do in someTransformation? You could just use a template if you just want to do something before or after calling the proc being passed.
Re: Is it possible to add a proc after a proc in a macro?
Then definitely check out the memo module, because it shows how you create, copy, and rename procs in a macro.
Re: Passing a pointer to an array to a c function!
Update: The results I talked about yesterday were obtained with Nim simply -d:release compiling but with quite some optimization for the C reference code. Today I cleaned up some minor lose ends and did some polishing (for both, C and Nim) and set Nim to compile with --opt:speed plus some checks disabled (which is a) unnecessary in this case, and b) fair because C has none of those at all). And - I hope you are seated properly - Bang, the algorithm implemented in Nim is on average 2% to 3% **faster than the C version!** And no that's not due to an error. I cross checked over 100K test vectors. The Nim implementation is correct. Kudos to @Araq and the Nim team!
Re: Is it possible to add a proc after a proc in a macro?
I think Steve Flenniken covers this or something very close to it in his [blog post about nim macros](https://flenniken.net/blog/nim-macros/)
Re: Can't return closure procedures from `closureScope` blocks
You can use this: proc foo: seq[proc: int {.closure.}] = var res: type(result) = @[] for i in 0..1: closureScope: res.add proc: int = 1 result = res for f in foo(): echo f() Run
Help upgrading macro to work in compileTime context
I'm stuck at the following. Is there maybe a way to detect that I'm resolving a macro for a compileTime context rather than a runtime context? I'm trying to upgrade the memo module. Normally, if memoizing in a runtime context, like the following, then the cache is initialized as such. proc complexProc(n:int):int {.memoized.} = # this is recursive... ... proc main() = var fastResult = complexProc(46) echo fastResult Run Cache initialized in `memoized()` macro: ... var cache = initTable[argType, retType]() # this is runtime-only declared ... Run Okay, now if I want this to work in a compileTime context, I just change `var fastResult` to `const fastResult`: proc complexProc(n:int):int {.memoized.} = # this is recursive... ... proc main() = const fastResult = complexProc(46) echo fastResult Run BUT! I must change cache initialization to be `compileTime`, which now makes it unworkable for runtime contexts: ... var cache {.compileTime.} = initTable[argType, retType]() # this is compileTime-only declared ... Run So, what kind of when/if statement should be there? I can't use `when nimvm` because it's always in nimvm. Alternatively, I could do the macro equivalent of a global cache if I ensure unique ident, but I don't like that I have to check for initialization on every call and can't rely on `{.global.}` to do the right thing at `compileTime`. Perhaps this is simply the way to go and settle for the extra if statement penalty. Caching that works in both compileTime and runtime contexts: proc complexProc(n:int):int = var cache {.global.}:Table[int,int]() # with the if, works in compileTime and runtime contexts if len(cache) == 0: cache = initTable[int,int]() # usual recursive code Run
Is it possible to add a proc after a proc in a macro?
I don't know how to put this into the right words, so let me give you an example: macro someTransformation(proc1: NimNode): NimNode = # create a "wrapper" proc for proc1 proc someProc(a, b: float): float = result = a * 2 + a / b someTransformation(someProc) # this should create a new proc based off of someProc Run Is this possible? AFAIK `someProc` will be passed as an `nnkIdent` to `someTransformation`, but I need to pass the AST of the proc itself.
Can't return closure procedures from `closureScope` blocks
proc foo: seq[proc: int {.closure.}] = for i in 0..1: closureScope: result.add proc: int = 1 for f in foo(): echo f() Run `(14, 7) Error: 'result' is of type which cannot be captured as it would violate memory safety, declared here: abc.nim(11, 1)` How am I supposed to append it to the result variable, then?
Nim-lang is added as supported language in repl.it!
Thanks to all supporters that voted for adding Nim in repl.it: hip, hip, horay, [it is done](https://repl.it/talk/announcements/Two-new-languages-Nim-and-Crystal/11886)! [Try it](https://repl.it/languages/nim)