118 points · by ibobev · 5y ago · 56 comments
The `with` syntax is new to me, but it looks useful for dealing with lots of subfields. Its also a good point that while Nim looks like Python, it very much isn't.
Nim is overall much better designed from a computer science perspective with saner scoping rules, etc, IMHO. That means that while Nim as a language is relatively complex, overall its easier to work with than C++ or even Python which have lots of special cases.
(Whereas in C++, you can pick your own subset for your own code, but as soon as you pull in a 3rd party library, you HAVE to understand the preprocessor, templates, often template metaprogramming, lambda capture rules if that library is built for them, etc).
Personally from trying it out, I like the speed resulting from the stricter module system. And there might be a little additional protection against bad builds resulting from it. But I assume it comes at a price in flexibility as well - I can't really tell since I quit after working 6 months in it.
- bounds checking enabled by default for arrays and strings
- real enumerations that aren't implicit converted into numeric types (fixed in C++11, if one bothers to use enum classes)
- enumerations can be used as indexes (C++ can work around this with enum classes and some boilerplate templates)
- most data conversions must be done explicitly
- ability to actually define subtypes that are enforced by the type system (C++ offers this, provided one does the required boilerplate class/template code)
- memory allocation by default doesn't require doing math with data sizes (C++ as well, yet there is too much malloc()/free() pollution in C++ code bases)
- no need to deal with pointers for out parameters, thanks reference parameters, which even in C++ there isn't any guarantee they aren't null, even though that would actually trigger UB)
- in some of the dialects, namely Modula-2 and Oberon and the languages derived from them, unsafe code requires explicit import of SYSTEM package
- if one wants to do crazy C like code, all the necessary gear is available, pragmas to turn off bounds checking, pointer arithmetic, unchecked casts, unions, mapping variables to explicit memory addresses, pointers to callbacks, it is everything there in the package. The original bare bones ISO Pascal wasn't no longer relevant in the mid-80's, unless the teacher didn't knew any better.
In fact, IBM i, z/OS and Unisys ClearPath have done pretty well with PL/S, PL.8 and NEWP, only adding support for C and C++ after the market started to care about POSIX support on mainframes.
Of course you can do things like bounds checking manually in C, and run with all sorts of warnings enabled, and you can use static code analyzers that uncover entie classes of bugs, but the whole point is to never get that far in the first place.
I worked with Borland's Pascal dialect for 10+ years, and it offered a great deal of protections while still allowing you to use unsafe constructs when you needed to get close to the metal (e.g. pointer arithmetic and even embedded assembly code). But "safe by default" solves a lot of problems.
It's not syntax, but rather a third-party macro[0], which it should be noted is inspired by a rather horrible feature of the same name in javascript. Though I guess the devil would be in the details.
D also has withs. Definitely in the "not necessary but nice to have" category of features, particularly when implementing state machines (i.e. statment soup)
You describe the events, transitions and even interrupts and this compiles to optimized goto statements with no allocation, no indirect function calls, no switch statement.
There are compile-time checks to make sure you don't have state with no transition from and it also produce a visual graph of your state and transitions.
It's been tuned to be used as the core of my high performance multithreading runtime so zero overhead compared to handwritten.
It is suitable for any state machine with zero alloc constraint and state machines can be composed (they are just a function).
I hope in the future to be able to add model checking using Nim Z3 integration so that we can reach Ada/Sparks formal verification prowess: https://github.com/nim-lang/RFCs/issues/222
wrt to the SMT integration, it's something that I would very much like to try in D, but it's quite difficult to work out exactly where to start (and means bolting a fairly big dependency on the compiler). There also seems to be a big gap in the literature between "practical" verification done "in" the widget being verified (e.g. SystemVerilog code) and more academic work where, effectively, a parallel structure is built which represents the semantics of the widget.
It's an interesting pattern to test language designs with. On the one hand expressive macros can be disastrous in big codebases, on the other hand a more "monadic" (implemented via passing some kind of object through a chain rather than algebraically) way is very template-intensive.
For this same reason I really dislike that in C++ you can just implicitly drop the "this->" to target class members. You can never tell at a glance what "foo = blah" does if you don't know whether "foo" is a member of "this" or not.
I think your page demonstrates what I mean in the "nested WithStatement" example:
Foo foo;
Bar bar;
Baz baz;
f(); // prints "f"
with(foo)
{
f(); // prints "Foo.f"
with(bar)
{
f(); // prints "Bar.f"
with(baz)
{
f(); // prints "Bar.f". `Baz` does not implement `f()` so
// resolution is forwarded to `with(bar)`'s scope
}
}
with(baz)
{
f(); // prints "Foo.f". `Baz` does not implement `f()` so
// resolution is forwarded to `with(foo)`'s scope
}
}
with(baz)
{
f(); // prints "f". `Baz` does not implement `f()` so
// resolution is forwarded to `main`'s scope. `f()` is
// not implemented in `main`'s scope, so resolution is
// subsequently forward to module scope.
}
WiI get that sometimes it can cut on a lot of repetition, but I think I would be fine if the syntax was more explicit while still avoiding repetition, for instance:
with (some_object) {
.some_member = 1;
.some_other = 4;
not_a_member = .some_method();
}
And beyond that make it non nestable (i.e. only the first level of "with" is taken into account) to avoid the situation above with complicated overloading.In my experience that would account for 99% of uses of `with` while making the code a lot more readable without requiring a lot of context to make sense of it.
Although frankly even that might arguably overkill, for dynamic languages or ones with type inference you might as well just do something like:
{
let v = &mut some_annoyingly.long.thing();
v.foo = bar;
v.baz();
}
It's almost the same amount of typing and you don't need any magic.My preference is Nim over D primarily since Nim compiles to C, but also overall the community appreciates syntax sugar but also has a decent sense as to when and how to use it. Most "sugar" in Nim needs to be explicitly imported. Literally `import sugar` is one of my first lines in many files.
let v = &mut some_annoyingly.long.thing();
v.foo = bar;
v.baz();
In the example you list (is that Rust, or D?), if it was in C or Nim and those were structs-within-structs and not pointers/refs then `.thing` would be copied to a new `v` (if I recall the semantics correctly). So you'd end up with a weird "logic" bug where updates would disappear. The Nim `with` macro solves that by modifying the generated code,MIC DROP!!
Wonderful article. Nim is a great programming language. I am using nim and c these days as my primary languages.
(Saying this as a fan of Nim and lean desktop apps in general, though also someone who really doesn't get web technologies.)
If you are looking for cross platform, very graphical, not necessarily accessible, there's NimX. I think there's also a port of Dear ImGui called Nimgui.
If you are ok with webby interface, there's a Nim port of zserge's webview.
There's a discussion on the Nim forum you might find interesting: https://forum.nim-lang.org/t/5632
I really despise the 'common' module approach because it scatters code that should be in the same file.
One workaround I use to avoid this is to break the cycle of type dependencies by replacing one of the dependencies with a generic parameter. This works great if the type that has a generic parameter only deals with opaque objects of that type.
Once concepts are fully supported (they may already be now), this pattern will be able to describe more complex relationships.
If you have circular dependencies then, in my opinion and experience, it unifies code that should be in the same file, and which was wrongly scattered among different ones.
I remember reading the release notes on Turbo Pascal 6 (or was it 5? or Borland Pascal 7) with a weird half-assed support for circular type dependencies among different units, and trying to find a case where putting all the definitions in one file (and avoiding all these issues) wouldn't make more sense to me. It's been 30 years since, and I haven't seen an example as such yet.
I acknowledge there's no accounting for taste, of course.