News for hackers. Stuff that matters.
Re:Mastering Nim – now available on Amazon by davidkunz on Wednesday June 29, 2022 @06:34PM (#31926517)
I absolutely hate Nim for spoiling pretty much all other programming languages for me. I wish it gained more traction. Congratulations on your book and keep up the great work, Araq. You did an outstanding job with Nim and I wish you all the best!
Re:Mastering Nim – now available on Amazon by jasfi on Thursday June 30, 2022 @01:00AM (#31928852)
I totally agree, Nim is great but it really needs more traction. I'm developing a web framework for Nim with batteries included (an ORM, for one), Nexus: https://github.com/jfilby/nexus
Re:Mastering Nim – now available on Amazon by rishav_sharan on Thursday June 30, 2022 @02:14AM (#31929176)
jasfi, I am actually following your project and wanted to take this moment to ask a couple of questions. How are you planning to solve for Postgres drivers/clients with async and pipelining support?

I had added nim std lib to the Techempower benchmarks, and while Nim shines in the plain text and other non db related requests, it comes at the very bottom as far as tests like Fortune go. When I did the work, there were no maintained/active Postgres drivers with async and pipelining support. And IMO, that wa the biggest reason why Nim hasn't had great benchmarking scores.

Re:Mastering Nim – now available on Amazon by jasfi on Thursday June 30, 2022 @04:37AM (#31930045)
Glad to hear you're interested in Nexus. The ORM currently uses db_postgres (from Nim's stdlib) only. I'm going to implement support for db_mysql and db_sqlite as well, it should be easy enough given how similar the API for the stdlib DB drivers are.

Actual work on DB drivers is out-of-scope for me though. I noticed there is an async Postgres driver available: https://github.com/treeform/pg. I don't know how well tested this driver is, and no mention of pipelining support.

It would be great to see Nim's web frameworks higher up on the Techempower benchmarks. Lack of such DB driver features usually comes down to time. The support for those features in stdlib would be a good bounty issue.

Re:Mastering Nim – now available on Amazon by prirun on Thursday June 30, 2022 @08:37AM (#31931611)
I did some testing with Nim a couple of years ago for HashBackup. Here's one of the db_sqlite issues I created:

https://github.com/nim-lang/Nim/issues/13559

I had much better luck with tiny_sqlite: https://github.com/GULPF/tiny_sqlite

Re:Mastering Nim – now available on Amazon by jasfi on Friday July 01, 2022 @02:03AM (#31942920)
It seems that issue is now fixed. Either way, Nexus will soon support multiple DB drivers, and I'll work on making adding new ones relatively straightforward.
Re:Mastering Nim – now available on Amazon by rishav_sharan on Thursday June 30, 2022 @06:14AM (#31930568)
I went with db_postgres as well. Unfortunately, it is not async. I did consider treeform's pg, but unfortunately i did not get any reply from him on my question so decided not to go with it for the TE benchmarks.

I think the lowest hanging fruit for db drivers would be to just wrap libpq, the default PG driver. Given Nim's c interop, that should be easy. I was planning to do that, but then life happened and I had to skip that project.

I would say, that if you are trying to create a very involved framework, you should look to solve this problem. Otherwise the current perf of db_postgres can be a big deterrent to a lot of potential users.

Re:Mastering Nim – now available on Amazon by jasfi on Friday July 01, 2022 @02:00AM (#31942906)
I agree that wrapping libpq sounds like a good idea.
Re:Mastering Nim – now available on Amazon by rishav_sharan on Friday July 01, 2022 @06:37AM (#31944587)
did a bit more digging and apparantly db_postgres wraps libpq already. So either nim is still using a very old version of libpq or it was never updated to use the async apis.
Re:Mastering Nim – now available on Amazon by giancarlostoro on Thursday June 30, 2022 @01:13AM (#31928912)
Nice one of the projects I want to work on is a simple text editor that lets you build and run. I think back to some of my favorite niche languages like Processing and Racket although I dont use them a ton when I do I love their interactive editors.

I am getting ahead of myself since I am still learning the language but I am coming from Python and various other languages so it has not been a complex transition.

Re:Mastering Nim – now available on Amazon by jasfi on Thursday June 30, 2022 @01:26AM (#31928956)
Nim also needs lots of example code for people to learn from. Even if people don't use Nexus, it's at least one more Nim project out there.
Re:Mastering Nim – now available on Amazon by giancarlostoro on Thursday June 30, 2022 @10:06AM (#31932498)
Do you have a Discord or anything? I'm interested in helping, I love Django and I would love to help any way I can.
Re:Mastering Nim – now available on Amazon by jasfi on Thursday June 30, 2022 @11:48AM (#31934215)
There's no Discord yet, just GitHub issues. Here's an issue to track this request: https://github.com/jfilby/nexus/issues/14

I'll email you regarding what/how to help.

Re:Mastering Nim – now available on Amazon by giancarlostoro on Thursday June 30, 2022 @12:59PM (#31935360)
Yes, that is fine, my email is in my profile. I'll look forward to hearing from you. I can help with the Discord if you need help.
Re:Mastering Nim – now available on Amazon by brightball on Wednesday June 29, 2022 @07:38PM (#31927091)
That’s how I feel about Elixir. Everything else just feels wrong now.

I’d be curious to get your Nim highlights.

Re:Mastering Nim – now available on Amazon by 127 on Thursday June 30, 2022 @01:11AM (#31928902)
My experience after a couple of weeks:

Pros: Extremely good C and C++ interop Significant indentation Very good performance Quick compilation Terse, but readable Easy cross platform Powerful macros and close to trivial DSL construction Very little code to do a lot of stuff Incoming future features look very promising

Cons: Hard to predict and control program performance, LTO does a lot of heavy lifting Immature ecosystem, because only few users. Many libraries have bugs, missing features or just a very clunky API. Or maybe they are deprecated or abandoned all together. Many different ways to do the same thing everyThingIsCamelCase, ambiguous variable names (subjective) Documentation and tutorials is mostly reading through the code and googling forum posts outside the core language You compile either for C or C++, some libraries simply won't work without C++ compilation

Re:Mastering Nim – now available on Amazon by jasfi on Thursday June 30, 2022 @01:33AM (#31928986)
Nim has several GCs available. For predictable performance you may want to use ARC. See: https://nim-lang.org/blog/2020/10/15/introduction-to-arc-orc...

For my contribution to the ecosystem see Nexus mentioned in another comment. If you see bugs please report them. Sometimes libraries are deprecated/abandoned because of new ideas, this happens in every ecosystem.

I personally use camelCase/PascalCase, but Nim is style insensitive, meaning you can call procs written in camelCase using snake case. However this is controversial and under discussion, my personal opinion is to make style sensitivity optional, with insensitivity the default.

Ambiguous variable names I hate, you won't see that in Nexus, if anyone does they should file an issue. That's really developer dependent.

For my part Nexus has some docs and a basic tutorial. But if people want more they need to ask for it. Why write docs nobody wants or will read? Anyway, this book (Mastering Nim) is one more definite step forward for learning Nim.

I think compiling to C is what mostly happens, C++ is probably an outlier. Even then, a C++ compiler isn't difficult to install if needed.

Re:Mastering Nim – now available on Amazon by planetis on Thursday June 30, 2022 @06:46AM (#31930776)
> Many libraries have bugs, missing features or just a very clunky API. Or maybe they are deprecated or abandoned all together.

For me missing libraries never stopped me, I could always port/wrap whatever I needed. However with the advent of AI assisted coding, working on libraries is even faster and at some point I hope I wouldn't spend much time rolling decent libraries that are ports of existing github projects.

Re:Mastering Nim – now available on Amazon by greybox on Thursday June 30, 2022 @06:02AM (#31930510)
I love nim, but I disagree with the "quick compilation" point here. The compilation is horrendously slow. (by default it compiles down to C, then to exe from there).
Re:Mastering Nim – now available on Amazon by cb321 on Thursday June 30, 2022 @08:16AM (#31931429)
As with most things, "it depends". If you compile very macro compile-time virtual-machine heavy code that can be slow. If you compile to the c++ backend then that backend process can be slow. If you invoke some macro that generates mountains of Nim code translated to continents of C code it can be slow. If you use the default `nim.cfg` that has gcc -Og it can take a lot longer than gcc -O0, for example. Meanwhile, if you use the `tcc` backend even without incremental compilation it can be close to immediate feedback:

    $ touch foo.nim
    $ /usr/bin/time nim c foo.nim
    CC: ../usr/lib/nim/lib/std/private/digitsutils.nim
    CC: ../usr/lib/nim/lib/system/dollars.nim
    CC: ../usr/lib/nim/lib/system.nim
    CC: foo.nim
    Hint: gc: refc; opt: none (DEBUG BUILD, `-d:release` generates faster code)
    36891 lines; 0.154s; 39.434MiB peakmem; proj: /tmp/foo.nim; out: /tmp/foo [SuccessX]
    0.24user 0.02system 0:00.27elapsed 101%CPU (0avgtext+0avgdata 44360maxresident)k
    0inputs+0outputs (0major+13676minor)pagefaults 0swaps
(I point my default backend compiler to tcc in my $HOME/.config/nim/nim.cfg.) There are some more details in this nim forum thread: https://forum.nim-lang.org/t/8677

Unless your standards are "single digit milliseconds per file like tcc itself, please!", 240 ms is not so bad. (EDIT: And this is just on some 2016-era Intel i7-6700k box, not some liquid helium cooled 10 GHz whatever.)

Re:Mastering Nim – now available on Amazon by nimmer on Thursday June 30, 2022 @06:35AM (#31930705)
It's much faster than C++, Rust, Go and many others.
Re:Mastering Nim – now available on Amazon by zimpenfish on Thursday June 30, 2022 @07:00AM (#31930875)
I'm assuming this must be for huge projects?

I just compiled my toy project (detecting anomalous parity in integers) in nim 1.6.6 (4.23s), go 1.17.7 (2.43s to create both aarch64 and x86_64 binaries), zig 0.8.0-dev.1140 (2.04s to create aarch64 and x86_64 binaries) and C via clang 13.1.6 (0.14s).

nim's compilation is 175% of Go, 207% of zig, and 3021% of clang.

Re:Mastering Nim – now available on Amazon by machinekob on Thursday June 30, 2022 @03:33PM (#31937428)
how are u compiling (optimization, custom compilation flags etc.?) In my case https://github.com/mratsim/Arraymancer big project compile under your 4.2s so or you have like 10k+ lines of codes with macros or you just pass some debug flags to compiler :D
Re:Mastering Nim – now available on Amazon by zimpenfish on Friday July 01, 2022 @04:53PM (#31952732)
Compilation command is:

    nim c -d:release --passC:"-flto" --gc:markAndSweep --out:ap ap.nim
I will add that once it's been run a few times, it does go a bit quicker (down to ~0.7s) but the Go also gets quicker (down to 0.28s for two outputs when files are cached.)
Re:Mastering Nim – now available on Amazon by nimmer on Thursday June 30, 2022 @07:17AM (#31931007)
No, even small files. You must have issues with your setup. Are you sure your compiler is not built in debug mode?
Re:Mastering Nim – now available on Amazon by zimpenfish on Thursday June 30, 2022 @07:46AM (#31931196)
How would I check that? I installed it through homebrew via https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/... which doesn't seem to mention debug mode anywhere?
Re:Mastering Nim – now available on Amazon by dom96 on Thursday June 30, 2022 @12:40PM (#31935098)
Run `nim -v`, if it's not in debug mode you should see "active boot switches: -d:release".
Re:Mastering Nim – now available on Amazon by zimpenfish on Thursday June 30, 2022 @02:07PM (#31936221)
Looks like it isn't in debug mode after all.

    $ nim -v
    Nim Compiler Version 1.6.6 [MacOSX: arm64]
    Compiled at 2022-05-05
    Copyright (c) 2006-2021 by Andreas Rumpf
    
    active boot switches: -d:release -d:nimUseLinenoise
Re:Mastering Nim – now available on Amazon by Rochus on Thursday June 30, 2022 @07:06AM (#31930930)
Thanks for the data; that's what I call an elegant rebuttal.
Re:Mastering Nim – now available on Amazon by hahaitsfunny on Saturday July 02, 2022 @10:42AM (#31959389)
No, it is not - Nim's compiler backend is extremely complex and full of cruft. I've been around Nim for 7+ years now and there's a reason the compiler was hard forked.

Nim's compiler is definitely not speedy, and this is why so much effort has been spent on incremental compilation, which tmk, still isn't working - https://github.com/nim-lang/Nim/issues/19757.

Re:Mastering Nim – now available on Amazon by boongeheYOE on Thursday June 30, 2022 @06:18AM (#31930595)
In comparison to languages like Rust and C++ it compiles pretty fast. Or maybe you built the compiler in debug mode.
Re:Mastering Nim – now available on Amazon by grayrest on Wednesday June 29, 2022 @08:23PM (#31927376)
I think Nim does DSLs particularly well and is the performant option for the Python class of scripty general purpose languages. I personally prefer more state controlled languages (Clojure, Rust) but I've considered pushing for Nim on shared/work projects. The main reason I haven't is network effects. The language niceties compared to other scripty general purpose languages aren't going to make up for the time spent writing all the libraries from scratch and I can't point to being able to control state as a motivation.
Re:Mastering Nim – now available on Amazon by elcritch on Wednesday June 29, 2022 @09:47PM (#31927880)
Odd, I've really yet to run into a key library that I needed and couldn't find. Not all libraries are listed in Nimble too. Granted I've not done much web server stuff in Nim. Sometimes a library needed a bit of polish, which is usually trivial given how concise they are. I've actually had better luck with it than say with Elixir on anything not web related. Occasionally I've needed to wrap or convert some C code on non-embedded projects.
Re:Mastering Nim – now available on Amazon by cercatrova on Wednesday June 29, 2022 @09:52PM (#31927915)
Elixir is not bad, but I definitely prefer static typed languages now after getting more into TypeScript. Not having types just feels wrong now.
Re:Mastering Nim – now available on Amazon by rapnie on Thursday June 30, 2022 @05:30AM (#31930321)
You might like this development around Elixir then: https://twitter.com/josevalim/status/1535008937640181760
Re:Mastering Nim – now available on Amazon by vasergen on Thursday June 30, 2022 @06:07AM (#31930532)
static types in Elixir would be fantastic
Re:Mastering Nim – now available on Amazon by throwawaymaths on Wednesday June 29, 2022 @10:44PM (#31928247)
Not having failure domains is worse than not having types IMO.
Re:Mastering Nim – now available on Amazon by eddyschai on Thursday June 30, 2022 @12:25AM (#31928711)
What are failure domains?
Re:Mastering Nim – now available on Amazon by throwawaymaths on Thursday June 30, 2022 @08:39AM (#31931631)
By default a green thread is the atomic failure domain. A 'panic' in this green thread is isolated, but sends an uncancellable notification to anyone who subscribes to it. You can tie multiple green threads together so that when one goes down the others do too.
Re:Mastering Nim – now available on Amazon by cultofmetatron on Wednesday June 29, 2022 @07:51PM (#31927168)
thats exactly how I feel when it comes to web frameworks. nothing else has anything good enough to give up all the nice toys elixir gives me.

haskell's types are cool

vuetify makes me consider nuxt

but only elixir has otp, immutable data structures, intercluseter message passing, channels, good performance, ease of use and descent ecosystem out of the box.

rust is the only other new language Im looking at but for things outside web development

Re:Mastering Nim – now available on Amazon by jasfi on Thursday June 30, 2022 @01:37AM (#31929000)
Elixir is indeed the most loved web framework according to the latest StackOverflow survey. I'll be looking to Elixir for ideas for Nexus (web framework).
Re:Mastering Nim – now available on Amazon by jasfi on Thursday June 30, 2022 @12:08PM (#31934578)
I meant Phoenix, which is built on Elixir.
Re:Mastering Nim – now available on Amazon by cultofmetatron on Thursday June 30, 2022 @02:33PM (#31936593)
a lot of what makes phoenix amazing is directly inherited from elixir which are sub-derived from beam.

Beam really is a marvel.

copy one write and immutable data structures mean you ca have garbage collection reserved for the termination of a thread whihc would be a nontrivial task in languages that encourage mutability cough go cough

Beam processes are a blessing

its also extremely tuned for creating threads that are super lightweight. consequently, scaling strategies that are optimal in elixir would be a TERRIBLE idea in other languages. You could make a dynamic cluster of node/go instances that communicate over rabbitmq and have a supervisor process that can keep track of their state and resurrection in case of a crash. Its going to be error prone and take weeks of fiddling to get right. beam gives you that out of the box. Its not perfect but its more than good enough for your first 10000 customers.

This benefit extends all the way up to Phoenix. because threads are trivial to create and the overhead is low, its a viable strategy to generate a stateful process for every user as they interact with the system, this is what allows liveview to be so powerful.

Each channel connection gets its own thread as well. A single machine can handle thousands of threads this way with full isolation. one thread crashing won't affect your other connections. nodejs cannot provide that. if a web-socket connection crashes a node instance, it ends the connection for every customer currently connected to that node.

Every attempt I've seen outside beam to recreate this revolve around OS level process which have a higher overhead and then they still need to create a mesasge passing protcol and supervision system. since OS threads are epensive, it wouldn't be a. good idea to try creating something like liveview as you'll overload your machine.

So you might say, "ok, I'll craete a multicluster virtual machine optimized for immutable data structures with a built in intra process messaging protocol for communication as well as a higher level super vision process. at that point, I'll say "congratulations, you just reinvented the beam, lets see if we can port other languages to it!"

anyways, if you're looking for something a little smaller for now, I highly recommend really understanding ecto and how it has you interact with databases. its easily the best database library I've worked with. precisely because it isn't a orm. It maps sql rows to elixir records which has much less of an impedance mismatch. its achieves almost zero friction between your database and your api.

Re:Mastering Nim – now available on Amazon by danielvaughn on Wednesday June 29, 2022 @07:25PM (#31926988)
Care to give a brief overview of what you find appealing? I've never looked into the language before.
Re:Mastering Nim – now available on Amazon by j-james on Thursday June 30, 2022 @02:09AM (#31929147)
I particularly like, from the syntax side:

- Python-style syntax with significant indentation

- Uniform Function Call Syntax: a.len() == a.len == len(a)

- Fully unqualified imports by default: which might seem scary to Python programmers, but works great in practice because of static typing

- All of the above makes code readable and succinct

And from a language features side:

- Compiles to C with all the architectural targets that come with it

- Compilation to C also allows for easy C interop: wrap function signatures and types and you're done

- This, in turn, means that Nim libraries can bootstrap off of the massive C ecosystem, while adding nicer APIs on top

- Extremely performant GC by default: optional Rust-style annotations can further improve performance, and you can remove all overhead and manually manage memory with C-style pointers if you'd like

- Useful compile-time templates and macros that can directly change the AST

The community is also active and helpful on IRC/Matrix/Discord/Gitter (bridged together)

Re:Mastering Nim – now available on Amazon by j-james on Friday July 01, 2022 @03:45AM (#31943562)
One really important thing I forgot to mention is how extendable Nim is - first class support for macros and AST manipulation is very powerful. This cuts back significantly on the amount of functionality that needs compiler magic.

So you'll see alternative (or new) implementations of core language features like async, threading, DSLs, typeclasses, traits, etc available as external packages, with just about the same user experience as if they were built into the stdlib or compiler.

Re:Mastering Nim – now available on Amazon by vegai_ on Thursday June 30, 2022 @07:31AM (#31931103)
>- Uniform Function Call Syntax: a.len() == a.len == len(a)

a.len() == len(a) seems like a good idea, but I'm not so sure about a.len... Isn't that a bit ambiguous?

Re:Mastering Nim – now available on Amazon by davidkunz on Thursday June 30, 2022 @01:56AM (#31929075)
Other comments already gave great examples but for me personally: - Concise syntax - Elegant language constructs (e.g. method call syntax) - Great macro system - You have control over what GC you use - Great performance - Everything is simple, it's easy to read other people's code
Re:Mastering Nim – now available on Amazon by pddpro on Wednesday June 29, 2022 @07:31PM (#31927030)
Even Rust?
Re:Mastering Nim – now available on Amazon by winrid on Wednesday June 29, 2022 @09:37PM (#31927821)
Nim gives you like 85% of the performance of Rust without having to worry about a borrow checker. It also solves concurrency problems via its use of Channels, like Go.

So it's just hard to justify using Rust, for me. Unless you're writing a DB or something where a GC is a no go. But Nim's memory management seems pretty good, and many things are stack allocated.

Re:Mastering Nim – now available on Amazon by warangal on Thursday June 30, 2022 @12:36AM (#31928751)
Even though Nim by default works with a GC to manage memory, I regularly use Nim to work with direct-memory/raw-pointers and can still have increased memory safety by using HOOKS and ARC. I have written about my experience here[1], in case somebody is curious.

1. https://ramanlabs.in/static/blog/raw_memory_management_patte...

Re:Mastering Nim – now available on Amazon by elcritch on Wednesday June 29, 2022 @10:01PM (#31927981)
Its nice being able to go all the way from fully GC reliant cyclic data-types with closures all the way to memory control-freak every byte and microsecond matters by using stack objects and 'var' parameters. Often you can keep 90% of the code and just tweak a few slow points.
Re:Mastering Nim – now available on Amazon by tsoj on Thursday June 30, 2022 @03:32PM (#31937406)
And for the stuff that matters, it is usually possible to tinker around enough to get it pretty much as good as Rust or C++.