209 points · by charlieflowers · 7y ago · 166 comments
[0] https://nim-lang.org/docs/manual.html#lexical-analysis-ident...
So MyNimName, mynimname, MY_NIM_NAME, My_Nim_Name, __MYNIM___name_, and any other variation you can think of are all the same name!
Most search tools have the option of case sensitive or insensitive search, but a search that does that and ignores underscores? Not too many of those outside the Nim world.
An interesting contrast is Nim's policy on tabs and spaces for indentation.
Spaces are fine. You can use as many as you want. Two, four, three, whatever. Nim doesn't care.
But Tabs? They are forbidden!
Unless you use this magic line at the top of each source file:
#? replace(sub = "\t", by = " ")
Now you get to use tabs!I feel most people who read about style insensitivity imagines all Nim code being written as a crazy mix of styles, while in reality Nim code is often more consistent in style.
And the tabs vs. spaces issue is simply to alleviate the issues with "how many spaces are a tab" which is impossible to guess when compiling code. The replace filter you mention above essentially just explicitly specifies how many spaces you intend a tab to be. That being said I would've liked it better if it forced tabs, that way you'd have one tab for one indentation level, and everyone could choose their own preference of indentation.
The problem with this is that in many code bases, you occasionally need spaces to align code/data in different lines to make them more readable and easily inspectable, e.g. within a long parenthesized expression spanning multiple lines. In that case, indentation tabs require nontrivial gymnastics, whereas spaces are consistent.
The files should always contain spaces - because behaving as if spaces are tabs is round-trippable even when every user uses a different setting. An editor could en-tab on load, de-tab on save, and everyone is happy (though I'm not aware of any editor that does this). If the file only has tabs, the converse "de-tab on load and en-tab on save" does not give you the same alignment flexibility.
In any case (pun intended?) it's the underscore insensitivity that makes it difficult to use standard search tools.
The motivation is good, if the intent is to get rid of ___unwanted___crap___ like this.
But if I, as a language designer, wanted to ban such identifiers, I would just go ahead and ban them, rather than making them equivalent to ones that do not have repeated underscores.
The principle is: don't make unwanted/undesirable forms equivalent to acceptable forms in hopes that people will then just stick to the acceptable forms when they discover that the the unwanted forms don't bring about the difference they were hoping for. People won't. People will go to town with the equivalence to make problems for other people.
People who don't know about the equivalences, or have forgotten, will be tripped up. Someone might think that a variable called __foo in an inner scope is different from an outer (or global) _foo, yet their definition will shadow _foo, with some behavior-altering consequences.
Nim takes a practical approach by banning tabs altogether; The "#?" hack you suggest just shows another great Nim feature - source code filters are standardized; You don't need a preprocessor/lex/yacc/re2c/swig with its own driver/makefile; it's all well documented and tracked within your Nim environment.
[0] especially in languages in which indentation changes program semantics, but even in e.g. C - where mismatch between indentation and curly brackets can let a bug like "goto fail;" hide in plain sight.
It's not as if some non-user of Nim announcing they'll avoid the language is any great loss.
Reminds me of all those "Cancel my subscription" letters to the editor in days past. Yeah, I'm the the "Time" magazine or whatever would tremble to know someone is cancelling...
I think that case insensitivity actually does make sense when working with symbolic identifiers. I've seen justifications for "Foo" and "foo" being distinct symbols; personally, I find that playing with casing like that reduces my ability to read code quickly.
Case insensitivity does have its downsides, but usually it's not as noticeable in VB.net because most development is done in VS, which automatically re-cases symbols to match the original definition.
[1] Number 6 based on search engine popularity according to https://www.tiobe.com/tiobe-index/ Also, anecdotally, it's used quite a bit in industrial automation.
Case insensitivity is a feature and it's meant to allow easy interfacing with C.
And it also encourages clean code.
With other languages you can have variables called "startdate", "start_date" and "startDate" in the same scope leading to bugs - especially when using completion in an editor - and poor readability.
In Nim the compiler tells you that you are redefining the same variable in 3 different places and you can go and give them more meaningful names.
There is a reason old languages were many times case insensitive but not anymore.
Regardless, this does not preclude the use of Nim any more than a regulation saying "do not use preprocessor macros" preclude C because you might accidentally use #define. It just means you have to verify that you adhere to those regulations. Here's one tool[0] you can use to make sure your unconditional requirements are satisfied.
Nim would probably be precluded anyway based on age and popularity, and rightly so for tightly regulated industries -- but given some time, assuming it does grow in popularity, there is no inherent reason Nim would be precluded from any project that allows e.g. C++, Java, JavaScript or Go.
If this is the goal, there are many alternative ways to solve this problem, making the language case insensitive is a pretty big hammer.
As an aside, is there some reason that using the same identifier with different cases is desirable?
It's not like that. The thing is that having case insensitive identifiers incentivices people to be inconsistent with their casing "because it doesn't matter" as one coworker that works in a case insensitive language said to me. "Whoops, I wrote 'vALUE'. Well, it doesn't matter; it works. :)" Thank god I don't have to touch that codebase. I do wonder on average how many different variations of casing is used for each variable and keyword.
Yes, there are always people who would do stupid stuff.
But we have decades of experience with Pascal, SQL, VB, VB.Net, Excel and variety of other languages. Decades. They each have their problems, but in wide practice, case [in]sensitivity is not one of them.
It is mostly for conventions, however different cases are generally used to represent different things. For example, in Python:
- ALL_CAPS: global variables
- PascalCase: for classes, and now that Python has typing annotations, types
- snake_case: everything else, from variables to function/method names
(This is not a rule, Python allows anything anywhere, so it is just a convention).
So for example, in Nim you couldn't have PERSON (global variable) and Person (class), however you could still have Person (class definition) and person (class instance). Of course, applying the code style from a language to another language is silly, however it is just to illustrate the value of case sensitive identifiers (if you think that my example is more or less valuable than having the flexibility of using a external library the way you want this is your choice).
BTW, nowadays I am studying Golang and I found it annoying that you capitalization is meaningful in Go, so person() is a private function however Person() is public. It is generally not bad, however sometimes I want to create a global private constant and can't simple call it MY_CONSTANT since this would make it public.
They could enforce case consistency instead of case insensitivity if that's the problem they're trying to solve, though.
Although, even then, consider that variables like 'inform' and 'inForm' might not mean the same thing...
Except you can't because Nim also ignores underscores in identifiers. Gotta make sure there is literally no way to disambiguate cases like this!
(I _want_ to like Nim but this single decision seems to me so spectacularly bad that it's a non-starter.)
You could ban using the same identifier with different cases, instead of treating them the same.
And Nim is one of those languages too. This is perfectly valid and frequently used:
type
Person = object
name: string
age: int
let person = Person(name: "you", age: 99)
echo person
You can try it in Nim Playground: https://play.nim-lang.org/#ix=20jSThis is NOT true.
The first character is case-sensitive in Nim, so examples like yours are not only possible, but frequently used even in Nim compiler and standard library.
Question is, should the language enforce you giving your variables a better name, or should convention? I think there are reasonable arguments for both.
auto * logger = Logger::getLogger("main");Feels like a poor design decision to me based on cost/benefit of user ergonomics.
I do it all the time, in numerical code. If you have two pairs of points, for example, it is very natural to name them "a,A,b,B"
And just to clarify once again, since I'm not sure who will read which reply: those are NOT the same identifiers in Nim!
let
a = 11
b = 22
A = 99
B = 88
echo a == A # false
echo b == B # false
Run the code in the Nim Playground if you don't believe: https://play.nim-lang.org/#ix=20krThink in reverse, is it a useful language feature to have MyNimName, mynimname, MY_NIM_NAME, My_Nim_Name, __MYNIM___name_ be different? When did you need this flexibility recently? Are we really so limited at naming things appropriately?
It's fine to make a big deal about something in theory but if it really was such a dumb idea it would have died over the decade Nim has been around. It was debated again prior to 1.0 and persisted, several good reasons are also mentioned in this thread. And it isn't even an original idea as brought up by several others.
If this is the reason someone doesn't try Nim then it's only unfortunate for them. Add this to the tabs vs. spaces, curlies vs. whitespace indentation or other so called concerns which make no difference in the grand scheme of things.
apiCall = __APICALL__
doneWell, unlike treeform, I like it to be:
api_call(system_call.inspect_something())
Which I find much more readable. What now?inb4 a Nim linter is written to force a consistent naming style throughout a codebase.
Already done:
https://github.com/FedericoCeratto/nimfmt
> Detect inconsistent variable/function naming
https://stackoverflow.com/questions/7375537/why-is-common-li...
Might be an interesting read for people who are thinking about using Nim and wondering how it compares to the C family, Python, Go, etc.
> The Pascal family of languages has an unpleasant, overly verbose syntax but fast compilers. It also has stronger type systems and extensive runtime checks make it far safer to use. However, it lacks most of the metaprogramming capabilities that I wanted to see in a language.
While Nim somehow is not appealing to me, this absolutely nails it.
To generate more talking points, somebody ought to write an inverse of dumpLisp: https://nim-lang.org/docs/macros.html#dumpLisp.m%2Cuntyped
In my honest opinion, discussion on syntax is a distraction. One of the reasons I like Lisp/Clojure is that there is very little syntax. I can just focus on my problem and writing good code.
I still haven't gotten around to picking it up though, these days I just use QuickJS for anything that needs to be reasonably portable and thin.
Maybe next side project...
I keep hearing about Nim and it sounds interesting, but I'm not sure I have the mental capacity right now to do a deep dive into the language and build something with it. I'd like to at some point soon though.
I like it. For me, it started out as a faster python that prevents typos. But it has really grown on me. I really like that I can share libs on server (compiling to c) and client side (compiling to plain javascript).
Basically any C library is also a Nim library with a tiny wrapper. That's a huge ecosystem! I also like how Nim can integrate with complex C++ libs that do virtual table call backs. It can also call objective-C functions .. you know the ones with [foo a:1 b:1] crazy syntax? Python was a glue language for me, but Nim can just glue more things.
JS too! In Nim you can just compile to plain JS, that other JS can call as well. Pass plain JS objects around. I basically replaced my JS code file by file - there was no need for a huge rewrite.
I know docker and kubernetes is "the thing" right now, but I just like that I can just scp the binary over. It just feels less complex. I feel like docker solved the distribution problem for python and node... I just don't have a problem to solve any more. It's just gone.
I'm also working on a game that I haven't quite moved to iOS/Android yet, but that is my plan. So far targeting the desktop with SDL and HTML5/Canvas with the JS backend works very well.
Does not appear to be any harder then sharing C or C++ libraries.
Do you use it in Pushbullet?
1) Its really easy to get started with. The default runtime has a GC for memory management, and the typing system is pretty similar to other popular languages. You can pick up the syntax in a day, the basic semantics in a weekend, and the more advanced semantics in a month.
2) Its fast and reliable. If the code compiles, it probably works, and it's probably a tier faster than a scripting language. Maybe not as bulletproof as Rust, but the ease of use is worth it for me. It can even reach C speeds, but that does require some advanced knowledge. Again, the default runtime is probably fast enough.
3) Powerful language features. I know there are a lot of people out there happy using C. I'm glad that works for them, but features like case objects, generics, and closures are part of the abstraction tools that I use to get stuff done. Nim also boosts what might be the best macro system for a static typed language, which is very helpful for framework developers.
It's a fairly small learning curve to be honest, especially if you're familiar with reading Python.
What I love about it is the small binaries.
I wouldn't choose to use it in production, mostly because of the developer experience. The documentation and tooling are both pretty bad, and the compiler errors can be arcane (especially when using generics, or when the error only surfaces during C compilation).
What doc issues are the biggest pain points for you right now?
(Getting good error messages for macros and meta-programming is notoriously hard, but we are trying!)
Edit: Email me if you'd like more info.
The biggest downsides right now as compared to Python are the overall lack of libraries, and lack of documentation (the core language is reasonably well documented, 3rd party libraries much less so).
As for the "mental capacity" I felt the same about finally getting into Rust. Spending some time with Nim actually helped me bridge the gap from Python to Rust.
I also want to point out that Nim does have an IRC channel (#nim) on freenode, which was helpful for understanding some of these things.
It's not bad, objectively speaking, but its design opinions are the polar opposite of mine.
However the flexible identifier rules bother me no end. But as soon as my editor of choice handles it transparently then I guess I might come round.
Curious if anyone in a DS role has tried it out. I'm certainly tempted to try it out, I'm sure I'm losing lots of performance in parts of my analytics pipeline due to Python being garbage slow.
I think today the best way to try Nim is for slow Python utilities vi nimpy: https://github.com/yglukhov/nimpy
You can also use Nim in Jupyter the following way: https://github.com/apahl/nim_magic
My next step would be to improve interop between Arraymancer tensors and Numpy ndarrays.
But also: many many moons ago I had Matlab code that was really slow (and I wanted to run 10^LARGENUM simulations for Monte Carlo purposes) and decided to learn Fortran. It was surprisingly easy to learn, and it did cut my running time from afternoons to minutes. But in the process I found out that my Matlab performance problems were mostly due to arrays being dynamically resized all the time -- while static memory preallocation could be hacked easily in straight Matlab.
As for Python, numpy is a fast embedded minilanguage written in C; the difference between "pythonic" and "numpythonic" is not emphasized enough in bootcamps, I think.
My experience with numpy was that it was 11x slower than visual basic access in excel. I was deali g with a lot of small matricies instead of a big one and I think that contributed.
I want to try julia in production. About to try out rust in production.
I just love it. Super productive to work with and in my case faster than the C++ framework I'm replacing.
The ecosystem of course is pretty small. But for me wrapping C code, porting something or reimplementing some libraries can be a lot of fun, too. In my experience every hole I've found was easily plugged so far.
[1] https://github.com/Vindaar/TimepixAnalysis (recently due to ..reasons.. all development happened on the open PR)
If I want to fit a particular kind of model I'll probably have to roll my own, meaning I have to be familiar with what the algorithm is doing and why, rather than just importing something straight from sklearn.
I also need to pay more attention to the code as I'm writing it instead of lazily debugging by trial and error in the REPL like I can with Python.
Basically, I wouldn't recommend it at work, but it's been fun and useful at home.
However, don't expect the included data frame to be fast. That in combination with a dynamic nature has been out of scope for me alone.
It was an interesting language, for me it felt like a stepping stone to something better. But the docs were really bad,
This does not match my experience. The #1 way I see to optimize Python is to not write Python at all, and instead to FFI out.
* to work in both Python 2 and 3 (no telling what other system's Python will be) * don't use any modules outside of stdlib (that requires install too)
Those aren't always requirements, but they are the reason I reach for compiled languages when I can't control the target environment where the program needs to run (or don't want to).
It also targets C, C++, Objective-C and Javascript, making it possible to run on platforms where using C is not the preferred choice.
It feels faster than Go, but I think part of that is down to Nim's compile output being verbose by default.
I have a couple of 10k+ LoC codebases.