when to use 'ref object' vs plain 'object'

2021-08-25 Thread Araq
> ref object would be almost never needed if Nim supported copy on write. Nim does support "copy on write" via custom =hooks. > The only problem is poor performance caused by copying large objects (which > copy-on-write may help solve), so you had to use ref, even if you don't need > it in

String expression parsing

2021-08-25 Thread Araq
> first, and then use strscan? First write a tokenizer and then a parser operating on tokens, that keeps things clean. I don't know which tools to use for these, I usually code them manually. NPeg and strscans can be helpful but avoid regular expressions.

String expression parsing

2021-08-25 Thread sky_khan
I've used your problem for trying [npeg](https://github.com/zevv/npeg) out . I guess I have too much time :) Disclaimer: This is my first try using Npeg. Use at your own risk Here you're: import npeg, strutils, tables type Dict = Table[string, string] let parser

when to use 'ref object' vs plain 'object'

2021-08-25 Thread ElegantBeef
Well given your follow up statement you could very likely use a procedure with `lent T` to get the exact semantics you're using safely without exposing a mutable reference or copy. For instance: var a = "SomeString" proc getSomeString: lent string = a assert

when to use 'ref object' vs plain 'object'

2021-08-25 Thread alexeypetrushin
> Generally always when we have "many to one" relations. No. You only need `ref` if you have **mutable** "many to one" relation. If it's read only, you don't care if it's copy or ref, because it behaves the same. The only problem is poor performance caused by copying large objects, so you had

Why does this, while incorrect, iterator( code always result in a crash of the playground?

2021-08-25 Thread dom96
Weird that this would crash the playground, it runs in Docker so shouldn't happen. Can you make an issue about it in ?

String expression parsing

2021-08-25 Thread Stefan_Salewski
For the SDT tool in PCB mode we allow users to create pads not only with the mouse, but also by entering the data in form of a string, which is some form of a batch mode. A PCB pad is basically a rectangle, with an attached number and a name. The text input string has this shape: Pad x1, y1,

when to use 'ref object' vs plain 'object'

2021-08-25 Thread Stefan_Salewski
Of course there are many cases where we need references and ref objects. Generally always when we have "many to one" relations. Examples are in the kids book, or see my SDT tool drawing shapes on the screen. Basically impossible without references. Note that many languages like Ruby, Python,

when to use 'ref object' vs plain 'object'

2021-08-25 Thread alexeypetrushin
> We tend to avoid ref object +1. ref object would be almost never needed if Nim supported copy on write.

using readFile at compile time break exceptions

2021-08-25 Thread giaco
I know that there's also macros.staticRead, but I was puzzled by this behavior. Is this a bug? proc f() = try: discard "FAILME".readFile except: echo "unreached" static: f() Run #

when to use 'ref object' vs plain 'object'

2021-08-25 Thread arnetheduck
We tend to avoid `ref object` unless it's semantically necessary (ie nodes in a DAG) - preferable is to use object always, and explicitly, where needed use `ref` \- for `type X` we might have a field `x: ref X` instead of using `ref object` \- inherently, this is more powerful because you leave

What's Nim's equivalent of Python's append() method for lists?

2021-08-25 Thread miran
1\. use triple backticks for code blocks 2\. you need sequences (`@[]`), not arrays (`[]`) 3\. you need `add`, not `append` 4\. read more:

What's Nim's equivalent of Python's append() method for lists?

2021-08-25 Thread robb1e
a.add(b)

What's Nim's equivalent of Python's append() method for lists?

2021-08-25 Thread konradmb
It's add()

What's Nim's equivalent of Python's append() method for lists?

2021-08-25 Thread mareklachbc
This is fairly close to a working `Python` code (except I added the variable declaration at the very beginning): `` var a = ["apple", "banana", "cherry"] var b = ["Ford", "BMW", "Volvo"] a.append(b) `` This runs into the `Error: attempting to call undeclared routine: 'append' ``. The syntax

a == b == c format

2021-08-25 Thread Araq
The problem is not Nim's parser or "simplicity", the problem is that `a == b == c` makes no sense as `==` already returns a bool, it would be a silly non-coherent special case...

a == b == c format

2021-08-25 Thread Hlaaftana
Rough macro to emulate this behavior: import macros macro chain(ex): untyped = var values, ops: seq[NimNode] var last = ex while last.kind == nnkInfix: # assuming left associativity ops.insert(last[0], 0) values.insert(last[2], 0)

How to make Nim more popular

2021-08-25 Thread ingo
> Also, a good GUI is important for beginners. Something like [PySimpleGui](https://pysimplegui.readthedocs.io/en/latest/) based upon [IUP](https://nim-lang.org/docs/iup.html) ?

How to make Nim more popular

2021-08-25 Thread herdingSheep
I found Nim when looking for a faster version of Python. Nim is as easy as Python making it a good entry point for beginners. I think it would be a good idea to target the young. Get the young using Nim and in a couple of years, you have a generation that thinks that anything else is just

Does Nim have anything equivalent to Ruby's puts, or Python's print, or Perl's say?

2021-08-25 Thread Stefan_Salewski
> I am sure there must be a way... I just haven't found it yet. Sorry but why do you not first read a tutorial and learn the language before fixing my typos: Of course fixing the typos is very nice, but I think not that important.

when to use 'ref object' vs plain 'object'

2021-08-25 Thread Stefan_Salewski
> so i'll have to review why > > proc box[T](): ref T = new(result); result[] = x # obj to ref conversion I think you wrote just bad code. Above line is just the first ugly part I saw, and you call it a few times. It is not a conversion, you allocate objects on the heap, of course that takes

Does Nim have anything equivalent to Ruby's puts, or Python's print, or Perl's say?

2021-08-25 Thread Yardanico
Well, it's not really a difference, it's to be expected - echo always adds `n` so if you also add your own `n` it'll output "First linenn" so there will be an empty line between the two.

Does Nim have anything equivalent to Ruby's puts, or Python's print, or Perl's say?

2021-08-25 Thread Yardanico
In your first case the output will be "First line\nSecondLine\n" In the second case "First line\n\nSecnd line\n" And the third case is the same as the first one.

Does Nim have anything equivalent to Ruby's puts, or Python's print, or Perl's say?

2021-08-25 Thread mareklachbc
Ah, yes, it's an interesting difference that echo ("First line") echo ("Second line") prints the two lines right under each-other, and echo ("First linen") echo ("Second line") puts an empty newline between the two, whereas echo("First linenSecond line") is the same as the first case. Now

Does Nim have anything equivalent to Ruby's puts, or Python's print, or Perl's say?

2021-08-25 Thread aEverr
Does echo not already put a new line after the string you echo?

Does Nim have anything equivalent to Ruby's puts, or Python's print, or Perl's say?

2021-08-25 Thread Yardanico
Uhh, I really don't understand your question - `echo` does add a newline after it by default.

when to use 'ref object' vs plain 'object'

2021-08-25 Thread rforcen
thanks for your comments, i've done some research using a below benchmark snippet: and timing for ptr/ref are almost identical: ref vs. ptr performance ptr: lap:14982 ref: lap:15027 so i'll have to review why in my particular case there's so big difference, is there any **profiling tool**

How to make Nim more popular

2021-08-25 Thread mareklachbc
> I can hardly imagine anyone abandoning its IDE of choice with tons of custom > settings and keyboards shortcuts etc. just to try some nobody know or use IDE > coming bundled with some language. Sorry, I didn't mean to imply that professional, or seasoned Nim developers would abandon their

Does Nim have anything equivalent to Ruby's puts, or Python's print, or Perl's say?

2021-08-25 Thread mareklachbc
This is a question more out of curiosity, than strict necessity, and I know Nim is inspired more by languages like C, than those of Ruby, or Perl, but still... Is there in Nim a built-in function that would act the same as **Ruby** 's `puts`, or **Python** 's `print`, or **Perl** 's `say`, one

a == b == c format

2021-08-25 Thread Yardanico
`if a == b and b == c and c == d: echo "ok" else: echo "bad" ` Run

Generics / function overloading for imported function

2021-08-25 Thread ynfle
Try use mixin or define another version of quote with another type in moda

How to make Nim more popular

2021-08-25 Thread auxym
Not convinced, unfortunately not a lot of people write native desktop GUIs nowadays.

a == b == c format

2021-08-25 Thread Kalbhairab
is there any way to write.. nim if a == b == c == d: else: ... Run

when to use 'ref object' vs plain 'object'

2021-08-25 Thread rforcen
https://github.com/rforcen/nim/tree/main/convexhull

Why does this, while incorrect, iterator( code always result in a crash of the playground?

2021-08-25 Thread Yardanico
Also (a bit offtopic) - you can insert code as code blocks because Nim forum uses RST (with some Markdown features) - see

Why does this, while incorrect, iterator( code always result in a crash of the playground?

2021-08-25 Thread mareklachbc
Oh, I get the difference between compiled and interpreted language now! So for example Python would execute the code right in the interpreter if I were to run it, which may cause the Python interpreter to either stop, or be stuck in an infinite loop, whereas Nim would simply launch the program

when to use 'ref object' vs plain 'object'

2021-08-25 Thread Stefan_Salewski
> my experience, ref is 40% slower than ptr this is basically due to the logic > associated in each ref access That is nonsense. My kids book has a section about value vs ref objects and pointers, and thre is also a Nim memory tutorial available on the learning page, see

when to use 'ref object' vs plain 'object'

2021-08-25 Thread Araq
> ref is 40% slower than ptr No, typically it's not. What benchmark did you use?

heap mgr improved?

2021-08-25 Thread rforcen
keeping track of all memory create/dealloc pairs -> **a nightmare** , i've been two days solving all the leaks, with ref everything worked with much less coding effort

heap mgr improved?

2021-08-25 Thread rforcen
after some fiddling with ref & ptr found ref 40% slower but mush easy and safer to use, doesn't require dedicated destructor and dangerous dealloc's

heap mgr improved?

2021-08-25 Thread Stefan_Salewski
If performance is critical one may consider using memory pools, which allow faster allocations when all objects have the same size. Mratsim knows the details. Note that your suggested seq[ptr T] may basically work, as the destructor can free the whole seq. But how would you do a dynamic app,

when to use 'ref object' vs plain 'object'

2021-08-25 Thread rforcen
ref is much safer and easy to use than ptr, as far as performance is not a big issue, according to my experience, ref is 40% slower than ptr this is basically due to the logic associated in each ref access (ref counter mgr., access, release) while ptr is only access but coder must manually

heap mgr improved?

2021-08-25 Thread rforcen
thanks, not really worth using ptr if performance is not a critical issue

Why does this, while incorrect, iterator( code always result in a crash of the playground?

2021-08-25 Thread Stefan_Salewski
> important that you understand the differences between "compile-time" and > "run-time". Unfortunately that has become harder for beginners in the last years. It has become common for beginners to ALWAYS type nim r mycode.nim or nim c -r mycode.nim

Interfacing with C++ shared library that uses macros

2021-08-25 Thread Araq
I don't understand why you mix .header and .dynlib. Usually you should pick one option out of the two. When you use .header you can often get away with "importing" macros without having to rewrite them, but it's dirty and reimplementing the macro in Nim is cleaner, IMHO.

Why does this, while incorrect, iterator( code always result in a crash of the playground?

2021-08-25 Thread Araq
The compiler does **not** crash and actually does not run your program (the OS does run your program after the compiler compiled your program). Your program when run never terminates. It's important that you understand the differences between "compile-time" and "run-time".

WriteLine end the line by \n Why not by \p (platform specific)

2021-08-25 Thread Araq
I use Windows myself all the time... "I don't care about Windows users" could not be farther from the truth.

distrying seq[ptr T] field

2021-08-25 Thread Araq
Yes, that's right. However, you should probably just use `seq[ref T]` and leave it all to the compiler...

tutorial to clone c++ library

2021-08-25 Thread Araq
As a starting point, wrap the header files via `c2nim --cpp --nep1 file.h`.

Generics / function overloading for imported function

2021-08-25 Thread trisub
I've done some more experiments and I have a follow up question. I changed my module A to include a somewhat less dummy implementation of `quote[T]` for `seq[T]`: # moda.nim import sequtils import strutils proc quote*(s: string): string = "`" & s & "`"