> For obvious reasons these systems tend to be programmed in C or C++. But 
> there's a lot of interest in using newer languages that compile to native 
> code without needing a big runtime library, primarily Rust.

@snej thanks for expanding the context. Maybe there needs to be a "embedded 
Nim" website. ;) I was going to include reasons I didn't choose Rust, but felt 
it was already too long. Rust has a lot of interest and momentum in embedded, 
but has some drawbacks.

The primary being drawback being that Rust relies on LLVM, which doesn't 
support many embedded targets in particular the ESP32. There's experimental 
Xtense/ESP32 support for LLVM, but then also have to figure out how to use the 
existing support libraries for the target (if possible). There's a lot of C 
code and weird build systems in the embedded world. Nim is excellent in this 
regard! Pre-compile to C and plug it into the existing system and hit the 
ground running with debuggers, real time OSes, etc.

The other big impediment is that the Rust standard library is very large and 
monolithic which isn't good for embedded (it hardcodes a lot of OS stuff I 
guess). There's been big improvements since I last toyed with Rust in that 
area, but I believe Nim's approach of only compiling what's strictly 
required/called results in lighter/smaller compiled code for embedded (not 
faster code per se).

So Nim provides many of the benefits of Rust, but is usable on most any 
embedded device or ecosystem. I find Nim less tedious to program too.

> Could you elaborate? I've only been using destructors (=destroy functions) in 
> my code. I've been assuming that finalizers are older/deprecated. If there 
> are any benefits of finalizers, I'd love to know.

@snej, destructor's only appear to work for objects though the docs say a 
finalizer will be auto-generated for refs of objects with destructors. 
Finalizers probably won't "go away" soon since they're used for ref's but might 
not be used directly as much. More importantly, a recent post [Returning 
objects from func with ARC](https://forum.nim-lang.org/t/7050#44326) did a good 
job showing how =destroy & =copy both get called and can result in a piece of 
"data" being destroyed multiple times. In this case, I want to called a special 
cleanup function for a C data structure but only once at the end, so finalizers 
made more sense. Though you could setup =destroy/=sink/=copy to do it too only 
once but that's beyond me at the moment...

Reply via email to