Hacker News new | ask | show | jobs
by girvo 1446 days ago
> I'm particularly keen on Nim's UFCS

One of my favourite parts about UFCS is how it can turn C libraries that I've had to bind into nice clean looking interfaces!

    esp_err_t sw_enableRx(SwSerial *self, bool State)
Becomes

    proc sw_enableRx*(self: ptr SwSerial, State: bool): esp_err_t {.importc: "$1", header: "<SwSerial.h>".}
Which when called is super lovely!

    var port = sw_open(params, etc)
    port.enableRx(true)
1 comments

The end result doesn't look anything like C! Really nice.

Nim almost feels like I'm writing a dynamic scripting language, except that I've worked with a typed python code base and it sucked because typing always felt taped on. With generics in Nim I feel like I'm duck typing, but my ducks are compile time checked!

I'm keen on experimenting with protocol oriented programming and as far as I understand, UFCS provides this really neatly to the language. I really like the idea of being able to define my own types and have them seamlessly work with functionality of other libs (and vice-versa) without needing to resort to a language feature like traits.

Absolutely! It's fantastic :)

One piece of advice I can give, is functional programming (which I am a huge proponent of), as defined by a tonne of chained/composed operations on sequences

   .map.filter.etc.etc()
Is not a good fit for Nim. `proc` is a really good reminder: its a procedural language, more than anything else!

You can still use a lot of FP ideas though, but one thing I always remind myself is at the end of the day Nim _is_ C, C is the output, so working within that idea helps a lot in terms of keeping things optimised with little effort on my behalf!