Hacker News new | ask | show | jobs
by Isomorpheus 2186 days ago
Why does Nim have "nil"?
1 comments

To represent a null pointer. Nim is a systems language (or at least can be used as one), and you need null/nil when working at that level.

Even Haskell has null pointers, if you know where to look:

https://hackage.haskell.org/package/base-4.14.0.0/docs/Forei...

Likewise unsafe Rust has null pointers, but I've never needed it or seen it used. (Same with Haskell).

You just mentioned using "nil" in a simple example which doesn't appear to be related to systems programming.

I didn't mention "nil" in a simple example. I wasn't the poster of that comment.
Oh, thought you were same poster. Well, that other person mentioned it in a context which doesn't appear to be related to systems programming.

Just checked Github for some of the top Nim projects.

Forum software: https://github.com/nim-lang/nimforum/search?q=nil&unscoped_q...

Web framework: https://github.com/planety/prologue/search?q=nil&unscoped_q=...

Twitter front-end: https://github.com/zedeus/nitter/search?q=nil&type=Code

These uses of "nil" don't appear to have anything to do with systems programming.

Fair point. I should mention that I'm not a Nim expert, and am not defending its design. It's true that 'nil' appears in Nim under both of its pointer types (GC traced "references" and untraced "pointers") -- which is unlike, say, Rust's safe references vs. unsafe (nullable) pointers. So 'nil' is not simply a systems level construct; otherwise you might expect Nim to have nillable pointers, but not nillable references.

You need nil/null when doing systems work -- and Nim is capable of that -- but I don't know the rationale for allowing 'nil' in higher level language features.

Thanks for the honest reply.

Nim interests me but this nil business seems questionable to me.

Just found this comment from the language creator,

https://github.com/nim-lang/Nim/issues/6638#issuecomment-487...

"We need the nil state to disarm pointers but that only means these can be nil and so would require a nil check before deref. Doesn't seem to be too hard nor too cumbersome."

Seems a bit dismissive to me