In this example:
import db_sqlite
type
Database* = ref object
db: DbConn
proc newDatabase*(filename = "tweeter.db"): Database =
new result
result.db = open(filename, "", "", "")
Run
What doe
You can use `ptr T` instead of `var T` then, but beware, it's unsafe. It's
better to do this instead:
proc outer(x: var string) =
proc inner(x: var string) =
x.add "abc"
inner(x)
var g = "x"
outer(g)
echo g
Run
The variable is sent as a var to a proc where it works fine. But I then in that
proc I pass it to another proc and in that one I get the error. I've also tried
passing it to the other proc without var (since in that data is only read) but
get the same error. Is there an alternative to var that w
You are probably trying to use a `var` parameter somewhere you shouldn't like a
closure.
In some situations I get the error message _" cannot be captured as it would
violate memory safety"_. I understand that this means nim is trying to stop me
shooting myself in the foot, but is there anywhere I can read an explanation
and if it is possible to do what I want without triggering this
@kidandcat Yeah, you are right, it's not an error, just a warning, sorry. I'm
used to C where a warning usually means I've done something wrong. @mratsim and
@rayman22201 thanks for explaining that, it seems like the issue has been
thought through, so there's no point raising it.
Anyway you stated you got an error accesing the global variable, if you don't
compile your program with --threads:on you will get only a warning and it will
compile correctly
As you noted, the stdlib asynchttpserver is not multi-threaded, but httpbeast
is multithreaded.
But, both libraries use the same core stdlib asyncdispatch (they were written
by the same author @dom96).
asyncdispatch supports both single threaded and multi-threaded environments.
This is why the
Sure, it's better to have a discussion. But I think the constraints are not on
the asynchttpserver module but directly on asyncdispatch, I may be wrong though
Thank you @mratsim. I did a lot of research and found an old github issue
discussing implementing threads in asynchttpserver. Araq closed it without
action noting that httpbeast is available, which is esentially a multithreaded
server. Do you think I should file an issue requesting the removal o
Iirc the gcsafe requirement was made so that it would not be breaking in the
future to make async threadsafe.
Thank you, that explanation makes sense regarding {.threadvar.} however I am
now confused as to why I need to worry about it at all. I am using **async**
http server which I am assuming is concurrent (single threaded), so everything
should be running in the same thread. This would explain why th
The Threadvar pragma, as its name explains, marks the variable as a thread
variable, then each thread you create will have its own copy of that variable,
thus, even if you error has dissapeared, you will find that if you modify that
variable in your thread A, and you then read it in your thread
er gc safe.
Adding the {.threadvar.} pragma to the global variable fixed the error, but I
don't know why. So:
1. How does accessing global variables make a function not gcsafe?
2. What does the threadvar pragma do to fix this?
Thanks.
Answering to myself (after, hopefully, having fixed the issue):
I was trying to access and change a ref object which was obviously not GC_ref
-ed before.
**Compilation options:**
-d:debug
-d:useSysAssert
-d:useGcAssert
--debugger:native
--stackTrace:on
--gc:refc
Run
**This is what I 'm getting:**
[GCASSERT] incRef: interiorPtrTraceback (most recent call last)
/Users/drkameleon/Docu
Explained in
[https://nim-lang.github.io/Nim/manual.html#templates-hygiene-in-templates](https://nim-lang.github.io/Nim/manual.html#templates-hygiene-in-templates)
Any variables declared in a template annotated with {.inject.} will be
accessible in the scope where the template is called, effecti
I see examples in the manual using the `{.inject.}` pragma but the behavior of
this pragma doesn't seem to be defined in the manual (or I missed it). Can
anyone provide a doc link or a brief explanation? Thanks!
It makes the variable public and importable from other module/files
Nice. I thought it had to do with pointers, references, etc. I was like omg lol
Example:
var
users* {.threadvar.}: seq[string]
Run
I'm brand new to nim. Just heard about it today and I'm in love with the syntax.
I googled but couldn't find anything. I think I read it was something to do
with exports, but not sure?
Thanks guys, it all makes sense now.
Yeah... you've got a while loop that is just calling an async procedure over
and over. This is just allocating new futures, the async proc doesn't have a
chance to run. If you `await` it, it will run.
You know that `wait` returns `Future[void]` and not `void`, yes? From this
knowledge, I can tell you that Future is an object that contains an error field
which `wait` will set if any exception is raised inside it. If you just call
`wait(1000)`, you will ignore this error field, meaning if the a
ached because of the asyncCheck in test()?
In that case, what does asyncCheck do?
proc wait(time:int){.async.} =
await sleepAsync(time)
echo "Done waiting:", time
proc test(){.async.} =
while true:
asyncCheck wait(1000)
waitFor test()
Run
It should not forbit it but allow it. Please notice that generics are almost
replaceable by () operator (it should be replaceable by [] operator but then it
only works when called explicitly, not by operator syntax):
template Sth(t: typedesc = int): typedesc =
type `Sth t` = o
AFAIK the compiler instantiates generics only when they are used. Here the
compiler considered the generated Shape as a generic type with no parameters,
which is legal, but seems unusable.
type Obj[] = object # no error
var x: Obj # Error: invalid type: 'Obj'
Thank you, that's correct. Isn't it a bug that the macro compiled without any
errors as long I would not use the generated type?
Hello,
It looks like you add generic parameters to your generated type even when there
isn't any. I suggest you put an Empty node when you have no generic parameters,
by changing the line 147 of your pastebin by:
var genericParams = if genericParXs.len == 0: newNimNode(nnkEmpty) el
Forgot to add that the type actually gets generated, it's just that I can't use
it.
variant Shape:
Cube(a, b: int, c: int)
None
Error
var x: Shape #error
Here's the entire abomination of a macro:
[https://pastebin.com/m2pLkCNs](https://pastebin.com/m2pLkCNs)
Sorry to hear it breaks for you, it works on my compiler.
But give quote + getAst at try, it's recommended over quote-do anyway, if I
recall.
import macros
macro decDumbType(): untyped =
let sym = genSym(nnkType, "Dumb")
template decl(ty) =
type Dumb =
Seems to give almost the same output:
> type
>
>
> Dumb112023 = ref object of RootObj
> contents: int
> method frobnicate(this112025: Dumb112023) {.base.} =
> echo "frobnicating!"
and I still don't understand why Dumb turns into Dumb112023, maybe I will later
`expandMacros` with its conversion to `typed` is really fragile with today's
compiler. Instead use this to see what's going on:
import future
import macros
macro decDumbType(): untyped =
result =
macros.quote do:
type Dumb = ref object of RootObj
Docs say:
> Quasi-quoting operator. Accepts an expression or a block and returns the AST
> that represents it. Within the quoted > AST, you are able to interpolate
> NimNode expressions from the surrounding scope
By this, I guess it likely means it pastes the sub-tree in at the given
position?
In my opinion, "direct hardware access in Nim" means File System API, Socket
API written by C. There are equivalent wrappers in Python (written by C and
wrappered by Python).
If you want to write some "direct hardware access" low-level API, you should
comply with the specification of these C AP
I don't think modern OSes will allow any user space application to access any
hardware directly. Any hardware access must be performed through system
calls(low level e.g. asm), which often encapsulated into function calls(higher
level).
But beside normal user space application, Nim can produce
Nim also allows [Assembler statements and
expressions](http://forum.nim-lang.org///nim-lang.org/docs/manual.html#statements-and-expressions-assembler-statement)
It means that when hardware has mapped itself to some data in memory, nim can
just read it and use it. I don't know how such things work in Python, but in
java it is always a big hassle, because java always needs these buffer objects
that on one hand give what you want to do with pointers, but w
I've been a Python programmer, far away from low-level stuffs.
40 matches
Mail list logo