4 ms·
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 nul
by gmfawcett 6y ago
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/Foreign-Ptr.html#v:nullPtr https://hackage.haskell.org/package/base-4.14.0.0/docs/Forei...
- Isomorpheus 6y agoLikewise 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.
- gmfawcett 6y agoI didn't mention "nil" in a simple example. I wasn't the poster of that comment.
- Isomorpheus 6y agoOh, 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=nil https://github.com/nim-lang/nimforum/search?q=nil&unscoped_q... Web framework: https://github.com/planety/prologue/search?q=nil&unscoped_q=nil https://github.com/planety/prologue/search?q=nil&unscoped_q=... Twitter front-end: https://github.com/zedeus/nitter/search?q=nil&type=Code 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.
- gmfawcett 6y agoFair 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.
- Isomorpheus 6y agoThanks 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-487633727 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
- gmfawcett 6y agoI agree that it's a sharp edge in the language. Or at least it seems to be -- I've written a little bit of Nim, and never encountered a case where I was actually bitten by 'nil'. The language does have a 'non nil' annotation that can be used when defining structs (and maybe in other parts of the language, I don't recall), so there must have been some thinking about the dangers of nil pointers. I would still recommend playing with the language for a while. It's quite practical, and the performance is very good. I've used it to write some utilities where I might otherwise use Python, but either needed more speed or needed to use it in a restricted environment where the Python interpreter wasn't available. The experience was mostly positive, and I would use it again.
- Isomorpheus 6y agoThanks, I intend to give it a fair try
- elcritch 6y agoP.S., you can annotate procedure to help ensure you can’t raise certain conditions in code. Imho it’s nice for writing sections of “safe” code.
- mratsim 6y ago`non-nil` was deemed too breaking to introduce for 1.0 or even as a default because if you tagged your input as `T not nil` the compiler was not powerful enough to prove that T was indeed not nil except in the most obvious cases. The new battleplan is to integrate the Z3 theorem prover into Nim as DrNim: - https://nim-lang.org/docs/drnim.html https://nim-lang.org/docs/drnim.html The first use-case will be proving array bounds at compile-time and elide them. Future use-cases include not nil tracking.