News for hackers. Stuff that matters.
Re:Nim v2.0 Released by netbioserror on Tuesday August 01, 2023 @10:09AM (#36956144)
Been happily crunching away at Nim in production. I'm working on what is mainly a data analysis and report generation tool, compiled as a CLI executable that gets called by server scripts.

Nim makes fast, small executables. It has an excellent heterogenous JSON data structure and a good dataframe library. It prefers the stack so strongly that dynamic data structures (sequences and tables, basically its lists and dictionaries) are pointers on the stack to heap data, where the lifetime is managed by the stack frame. I don't think I have any dynamic references anywhere in my program, and don't have to worry about GC at all. The type system is simple, sensible, and guides you to correctness with ease. Nim also defaults to referential transparency; everything is passed immutably by-value unless you opt out. Generics are powerful and work exactly as you expect, no surprises. Universal function call syntax is ridiculously powerful: You can write the equivalents to methods and interfaces on types just by making procedures and functions that take a first parameter of that type; not needing those abstractions greatly simplifies and flattens code structure. It's just procedures and objects (functions and structs) all the way down.

It's been a real joy to work with and reminds me of when I discovered D back in the day, only it's even better. If you imagine native-compiled type-annotated Python where nearly 100% of your code is business logic with no cruft, you're getting close to the Nim experience.

Re:Nim v2.0 Released by pavlov on Tuesday August 01, 2023 @10:28AM (#36956362)
> “It prefers the stack so strongly that dynamic data structures (sequences and tables, basically its lists and dictionaries) are pointers on the stack to heap data, where the lifetime is managed by the stack frame.”

Isn’t that the same as a C++ vector or map on stack? They allocate internally as needed, and the whole container is destroyed when it goes out of scope.

Re:Nim v2.0 Released by remram on Tuesday August 01, 2023 @10:45AM (#36956602)
Agreed, only being able to put pointers on the stack, no data, would make me think it "prefers the heap".
Re:Nim v2.0 Released by netbioserror on Tuesday August 01, 2023 @11:03AM (#36956884)
Dynamic data structures by their nature have to be allocated to the heap. What I mean by "prefers the stack" is that you don't have to make a managed ref and dereference a managed pointer type. You just make a `seq[int]`, use it as a `seq[int]`, and pass it as a `seq[int]`, just like stack data. Behind the scenes, it has a unique scoped pointer with no mental overhead.
Re:Nim v2.0 Released by jitl on Tuesday August 01, 2023 @11:26AM (#36957234)
Sounds like a vector/array/list in any other language after C++, like Go slice, Java ArrayList, Javascript array, Python list, Rust vec. Is there something I'm missing?
Re:Nim v2.0 Released by gcoakes on Tuesday August 01, 2023 @11:35AM (#36957393)
I think they're discussing the lifetime of that heap data, not whether the data is heap allocated.
Re:Nim v2.0 Released by chc on Tuesday August 01, 2023 @12:14PM (#36958041)
I don't see them drawing any distinctions from C++ or Rust there either. It really sounds to me like most of their low-level experience is in C, where the contrasts they appear to be drawing really do apply.
Re:Nim v2.0 Released by kragen on Tuesday August 01, 2023 @04:21PM (#36962164)
it depends on what you mean by 'dynamic' and 'stack'; certainly, outside of nim, you can allocate a list of, say, integers entirely on a stack in any of the following cases:

- the size of the list is known when you create a stack frame, as in c:

    int xs[n] = {0};
- when the list grows, it grows in that subroutine and not some callee, for example using alloca();

- the list is built on a stack that isn't the one you have to pop your return address off of; examples include perl's data stack, ada's secondary stack, forth's operand stack, forth's dictionary, or an mlkit region. in these cases you can even return the dynamically built structure to a caller;

- each new callee adds some fixed number of items to a linked list, such as, in c

    void with_fill(color *c, 
        env *e, 
        void (*cb)(void*, env*), 
        void *userdata)
    {
      env ne = {
        .prop = PROP_FILL_COLOR,
        .val = c,
        .parent = e };
      cb(userdata, &ne);
    }
look ma, no heap
Re:Nim v2.0 Released by arc619 on Tuesday August 01, 2023 @12:40PM (#36958459)
Types are stack allocated by default.

"var data: MyObject" is on the stack. "var arr: array[1000, MyObject]" is allocated on the stack sequentially.

Only dynamic seq or ref types use the heap by default.

Re:Nim v2.0 Released by pavlov on Tuesday August 01, 2023 @01:00PM (#36958807)
So, same as C++.
Re:Nim v2.0 Released by netbioserror on Tuesday August 01, 2023 @10:52AM (#36956704)
Basically, but it requires no extra syntax. `var some_seq = @[1, 2, 3, 4]` is a stack-managed sequence. That's all there is to it. There's no unwrapping any pointers or boxes or what-not, the type is just `seq[int]`. Put another way, things that have become best practice in C++ are default in Nim with no syntactic noise.
Re:Nim v2.0 Released by CyberDildonics on Tuesday August 01, 2023 @11:31AM (#36957322)
There's no unwrapping any pointers or boxes or what-not

That doesn't happen by default in C++ either.

std::vector<int> some_seq{1, 2, 3, 4, 5, 7};

Re:Nim v2.0 Released by jcelerier on Tuesday August 01, 2023 @11:35AM (#36957397)
or even just

     std::vector some_seq{1, 2, 3, 4, 5, 7}; 
nowadays (for a value of nowadays that is 5 years old for GCC and 6 years old for Clang)
Re:Nim v2.0 Released by seertaak on Tuesday August 01, 2023 @02:50PM (#36960604)
Indeed there's no question that Nim is basically following C++'s lead on this. Nim iirc always had constructors and destructors. Final piece of the puzzle is move semantics, and I recall a blog post where Araq came up with something very similar.
Re:Nim v2.0 Released by shirleyquirk on Tuesday August 01, 2023 @03:16PM (#36961069)
yes, Nim has move semantics, but takes care of you more than c++ does. for example, if you use an object that was previously moved, you dont get garbage, the compiler turns the first move into a copy (and tells you)

the relevant docs are here: https://nim-lang.org/docs/destructors.html

Re:Nim v2.0 Released by echelon on Tuesday August 01, 2023 @10:53AM (#36956725)
Same as Rust. Vec and HashMap are stack pointers to the heap-allocated container storage space.
Re:Nim v2.0 Released by seertaak on Tuesday August 01, 2023 @02:47PM (#36960556)
It very much is, and the point is, it _used_ to be more like java. Araq basically pulled off a very daring switchover from reference based language system to a value based one.

So now the language can credibly claim the same as c++ - no room left closer to the metal. But it's packaged in a much nicer syntax (imho), and has features like macros which we can expect I'm C++ in maybe 10 years, if we're lucky.

Re:Nim v2.0 Released by 999900000999 on Tuesday August 01, 2023 @11:29AM (#36957293)
It does indeed look like Python!

I hope it gets more popular, seems like a much much easier to use Rust

Re:Nim v2.0 Released by shmageggy on Tuesday August 01, 2023 @12:00PM (#36957820)
This sounds great. How is the package management story, and how robust is the ecosystem currently?
Re:Nim v2.0 Released by FireInsight on Tuesday August 01, 2023 @03:21PM (#36961158)
You can check out the list of Nim packages at https://nimble.directory
Re:Nim v2.0 Released by m0do1 on Tuesday August 01, 2023 @03:28PM (#36961298)
This provides a good overview of packages for various applications: https://github.com/ringabout/awesome-nim

I think most of them are available via nimble.

Re:Nim v2.0 Released by shirleyquirk on Tuesday August 01, 2023 @03:28PM (#36961310)
The ecosystem is smaller than some, but wrapping libraries is mostly automatable, and there is tooling to assist interop with python, c, and c++
Re:Nim v2.0 Released by Zamiel_Snawley on Tuesday August 01, 2023 @01:00PM (#36958790)
You have convinced me to look in to Nim! Can you speak to the build system(s)? CMake is the bane of my existence.
Re:Nim v2.0 Released by PMunch on Tuesday August 01, 2023 @01:20PM (#36959138)
Another Nim user here. Typically you just build the project by `nim c <myProject.nim>`, since Nim has such a strong macro system a lot of typical build stuff is just done with macros. Of course there's also the default Nimble package manager which allows you to list dependencies and tasks using Nim itself. This means that if you know how to write Nim managing the build system is a breeze.
Re:Nim v2.0 Released by elcritch on Tuesday August 01, 2023 @02:57PM (#36960748)
Ugh, I just got done spending months fighting CMake before moving back to a position using Nim!

You can also compile C projects with Nim like bearssl [1]. Nim takes care to compile the C files and recompile them when config flags change. It's actually really nice.

1: https://github.com/status-im/nim-bearssl/blob/99fcb3405c55b2...

Re:Nim v2.0 Released by netbioserror on Tuesday August 01, 2023 @04:18PM (#36962120)
Gotta be honest here, I use a pretty simple Makefile. I don't have anyone else working on this program, so I can afford simplicity.
Re:Nim v2.0 Released by inv2004 on Tuesday August 01, 2023 @06:05PM (#36963814)
I did a lot of nim small projects. All of them are on nimble build system: https://github.com/inv2004