Capturing a context variable in public procs

2020-11-13 Thread spip
I've simplified my problem to present it in the forum. Presently, I'm not using a template but a macro because configuration options must be processed before creating the context variable. And I've a few hundreds user functions, split in multiple files that I have to include. Documenting them al

Capturing a context variable in public procs

2020-11-13 Thread slonik_az
Attach all your documentation to the exported template `math`. Problem solved.

Capturing a context variable in public procs

2020-11-12 Thread spip
Yes, that's [true](https://play.nim-lang.org/#ix=2DXj). Probably simpler than writing the decorating macro, but the documentation comments from the inner procs won't be taken into account by `nim doc`? I'm still thinking about the easiest syntax for users...

Capturing a context variable in public procs

2020-11-12 Thread slonik_az
@spip For the record, another simple but wrong solution Error: 'export' is only allowed at top level This one is easy to make right. The `+` and `*` operators are defined and used inside the block. You do not need to export them. Simply remove the export symbol from them and you are done.

Capturing a context variable in public procs

2020-11-12 Thread Araq
Document it well that the convention is to call the parameter `ctx` and then have: {. pragma: libXXX, importc, dynlib: libName, cdecl .} proc libMult(ctx: Ctx; a, b: Mat): Mat {. libXXX .} proc libAdd(ctx: Ctx; a, b: Mat): Mat {. libXXX .} template `*`(a, b: Mat): Ma

Capturing a context variable in public procs

2020-11-11 Thread spip
Inspired by the `with` [macro](https://nim-lang.org/docs/with.html#with.m%2Ctyped%2Cvarargs%5Buntyped%5D), I'm thinking of writing a macro to inject the `ctx` into all `libXXX` calls while walking the AST. But in order for the procs to compile, I would have to write them like: # F

Capturing a context variable in public procs

2020-11-11 Thread spip
For the record, another simple but wrong solution: > It's possible to not use templates if you define your DSL in the math body: template math*(body: untyped) = block: proc `+`*(a, b: Mat): Mat = result = libAdd(ctx, a, b) proc `*`*(a, b: Mat)

Capturing a context variable in public procs

2020-11-10 Thread spip
>From what I understand, I will have to go with the macro route and manage the >full DSL language syntax. I have to think about it before jumping as my DSL >can be mixed with Nim code and only the calls to the library needs to get the >context variable (I used a math syntax in my example but the

Capturing a context variable in public procs

2020-11-10 Thread mratsim
It's possible to not use templates if you define your DSL in the math body: template math*(body: untyped) = block: proc `+`*(a, b: Mat): Mat = result = libAdd(ctx, a, b) proc `*`*(a, b: Mat): Mat = result = libMult(ctx, a, b)

Capturing a context variable in public procs

2020-11-09 Thread slonik_az
Nim's stdlib has `with` macro that seems to be what you need

Capturing a context variable in public procs

2020-11-09 Thread spip
I'm working with an external library that requires passing a context variable to all library calls. As I'm writing a DSL to ease using this library, I'd rather hide this context variable to the user of the DSL. Is there a way to achieve like a closure capture variable for public procs? For inst