Generic ref variables/fields?

2018-03-12 Thread StasB
Is there a traced equivalent to Nim's `pointer` (or C's `void*`), or do ref variables have to be concretely typed?

Natural type and overloaded procs

2018-03-12 Thread nucky9
I can't figure out why the following overloaded procs work: import sequtils proc newSeq2d*[int](x, y: int): seq[int] = result = newSeq[int]((x * y) + 1) result[0] = x proc newSeq2d*[uint8](x, y: int): seq[uint8] = result = newSeq[uint8]((x *

Re: Iterator invalidation

2018-03-12 Thread mratsim
Yes, the arguments must be immutable even after the for in that case. Seems like the syntax is this: iterator foo[T](s: seq[T]{call,`let`}): T = var i = 0 while i < s.len: yield s[i] inc i

Re: Iterator invalidation

2018-03-12 Thread StasB
That's an interesting approach, and it would be sufficient in some cases. Thanks.

Re: fileExists and existsFile. This made my day

2018-03-12 Thread dom96
lol wtf. Why have this synonym, I thought one of them was deprecated and was about to say "This is why all deprecated procs need to be removed before v1.0 is released"...

Re: Iterator invalidation

2018-03-12 Thread StasB
@mratsim: Could you elaborate on the third option, please? The following doesn't compile: iterator foo[T](s: seq{call,`let`}[T]): T = var i = 0 while i < s.len: yield s[i] inc i Whatever the correct syntax is, wouldn't this restrict potential

Re: Iterator invalidation

2018-03-12 Thread mratsim
Some ideas from the future Nim 1\. Writetracking: [https://nim-lang.org/araq/writetracking.html](https://nim-lang.org/araq/writetracking.html) 2\. Iterator returns a lent type so that mutation is a compile-time error:

Re: Iterator invalidation

2018-03-12 Thread twetzel59
Cool. Thanks for putting some thought into this I suppose that unrestricted referencing is one of Nim's design decisions in the language as a whole as well. It is perfectly valid to hold multiple mutable references to one object. Data can change under your feet at any time with this setup, so

Re: Need help with async client/server

2018-03-12 Thread r3d9u11
I thought that all code into async function will be async too my bad. A lot of thanks for all for your clarifications and hints!

Re: Iterator invalidation

2018-03-12 Thread twetzel59
Thanks! I hadn't thought of the effect system hmmm

Re: Iterator invalidation

2018-03-12 Thread StasB
> Should I/can I ever count on an iterator in Nim to handle the structure being > changed during iteration? Not unless the implementation promises to handle it, I suppose. There's nothing implicit in an iterator that stops it from being affected by changes in the structure. e.g.

Iterator invalidation

2018-03-12 Thread twetzel59
Hello, I have a conceptual/style question here. In C++ (and presumably some other languages), there is a concept called "iterator invalidation". The basic idea is that while iterating a structure, changes to the structure could break iteration, leading to a loss of memory safety. It is also

Re: Need help with async client/server

2018-03-12 Thread dom96
In regards to some other things written here: > I'm "pretty sure" that I observed in Windows that asyncdispatch would use > multiple different threads to run the code. It doesn't, and it cannot do that unless you pass `--threads:on`. This is assuming that Windows doesn't do something weird

Re: Need help with async client/server

2018-03-12 Thread dom96
Indeed, I was going to mention the ChatApp example from Nim in Action. Pretty sure it implements precisely what you're trying to implement. See both server and client here:

Re: How do I read BMP file header directly to type?

2018-03-12 Thread oswjk
If you want to use a library, [NESM](https://github.com/xomachine/NESM) could be of use. NESM creates the serialization and deserialization procs for you automatically based on your (a bit augmented) types. You could use it like this: import nesm import streams

Re: Need help with async client/server

2018-03-12 Thread enthus1ast
r3d9u11 a few unsorted things: * no need for c.getFd.getPeerAddr(Domain.AF_INET) use acceptAddr [https://nim-lang.org/docs/asyncnet.html#acceptAddr,AsyncSocket](https://nim-lang.org/docs/asyncnet.html#acceptAddr,AsyncSocket) * waitFor starts its own poll loop (nothing else will run

Re: Need help with async client/server

2018-03-12 Thread 2vg
In addition, recv is executed during sleepAsync, but it is blocked immediately by readLine (). It will not be a solution because we can not receive messages during blocking.

Re: Need help with async client/server

2018-03-12 Thread 2vg
Probably because stdin.readLine() is a blocking call, the event loop is blocked and other tasks can not be executed. wait well in a separate thread, or such a library will be useful [asynctools/asyncpty](https://github.com/cheatfate/asynctools/blob/master/asynctools/asyncpty.nim)

Re: Need help with async client/server

2018-03-12 Thread r3d9u11
ok, problem solved. just need to wait before sending data. I don't know why: proc sendClientMsg (c: AsyncSocket) {.async.} = while true: waitFor sleepAsync(1000) let msg = stdin.readLine() echo "<- " & msg await c.send(msg & "\c\L")

Re: Need help with async client/server

2018-03-12 Thread r3d9u11
something wrong when I tried to read data from stdin! this code works fine: client.nim proc readServer(c: AsyncSocket) {.async.} = while true: let msg = await c.recvLine() echo "-> " & msg proc sendClientMsg (c: AsyncSocket) {.async.} = while

Re: Need help with async client/server

2018-03-12 Thread mashingan
> why code with loop doesn't work? While I don't know the exact reason too, I think you need separate thread which handle the readline like this [https://github.com/mashingan/nim-etc/blob/master/cond_loop.nim#L58-L67](https://github.com/mashingan/nim-etc/blob/master/cond_loop.nim#L58-L67)

Re: Need help with async client/server

2018-03-12 Thread monster
I tried a few things, like "newAsyncSocket(buffered = false)", but it was no use. All I can say is, it doesn't work for me either.

Re: Concepts: how to check if a field exists without reading it?

2018-03-12 Thread mashingan
Using nimsuggest, the first snippet already error when calling `conToInter(5)`

Re: Need help with async client/server

2018-03-12 Thread r3d9u11
this code works fine: proc sendClientMsg (c: AsyncSocket) {.async.} = #while true: let msg = stdin.readLine() echo "<- " & msg await c.send(msg & "\c\L") why code with loop doesn't work? how to organize parallel/async read/write?

Re: Concepts: how to check if a field exists without reading it?

2018-03-12 Thread StasB
This seems to work, but it's kinda dirty, and the error messages aren't great: {.experimental.} type Vec2 = tuple[x, y: float] Kinetic = concept g, type T T.pos is Vec2 T.vel is Vec2 IKinetic = object Box = object

Re: Need help with async client/server

2018-03-12 Thread r3d9u11
well, I changed code. server.nim: import asyncnet, asyncdispatch, nativesockets var clients {.threadvar.}: seq[AsyncSocket] proc processClient(client: AsyncSocket) {.async.} = while true: let msg = await client.recvLine() for c in clients:

Re: Need help with async client/server

2018-03-12 Thread monster
I haven't tried your code, but one thing I would check, is that the problem is not caused by the threadvars. I'm "pretty sure" that I observed in Windows that asyncdispatch would use multiple different threads to run the code. So you might set the threadvar in one thread, but the code that

Re: How do I read BMP file header directly to type?

2018-03-12 Thread mratsim
Also you might want to take into account the endianness of BMP.

Re: Concepts: how to check if a field exists without reading it?

2018-03-12 Thread StasB
I'm overloading it for a sort of proxy object which contains a pointer to a vtable with field offsets and type information, to give seamless access to the fields of the target through the proxy. The entire thing is generated by a macro, so I could simply generate getters and setters as you

Need help with async client/server

2018-03-12 Thread r3d9u11
Hi. I'm trying to write simple async network client and server via AsyncSocket (by [sample from manual](https://nim-lang.org/0.13.0/asyncnet.html#examples-chat-server)). But client and server don't read any data. What I'm doing wrong? server.nim: import asyncnet, asyncdispatch

Re: Compiler crash when pointers to generics from template

2018-03-12 Thread mratsim
Probably because the overloaded PresentModeA is not generic so you can't use this syntax `PresentModeA[Foo]`, try with that: proc PresentModeA*[T: bool](d: T) = ...

Re: Defining variable in nim.cfg

2018-03-12 Thread mratsim
Yes you can, feel free to check Arraymancer `nim.cfg`: [https://github.com/mratsim/Arraymancer/blob/master/nim.cfg](https://github.com/mratsim/Arraymancer/blob/master/nim.cfg) @libman: Unfortunately shell scripts would not work on all Windows for example.

Re: Concepts: how to check if a field exists without reading it?

2018-03-12 Thread mratsim
Do you really need to overload `.`? That would make any further development of your codebase really tricky. If you want custom getters and setters, you can follow this: [https://nim-lang.org/docs/manual.html#procedures-properties](https://nim-lang.org/docs/manual.html#procedures-properties)

Re: Nim T-shirts and mugs now available

2018-03-12 Thread JohnS
Very cool, ordered.

Re: Before you start !

2018-03-12 Thread Diabolik
Thank you for your feedback I will buy the book and started to enter the universe of NIM ^^

Re: Defining variable in nim.cfg

2018-03-12 Thread Libman
A long long time ago we had this crazy thing called shell scripts. We used them to set environmental variables, build complicated command lines, etc...