News for hackers. Stuff that matters.
Re:Null References: The Billion Dollar Mistake by zozbot234 on Saturday January 11, 2020 @08:54AM (#22019854)
> Rust seems to be the only one that mostly circumvents this problem.

The Rust hype is getting ridiculous here. There are plenty of languages with non-nullable references as first-class, and optionals for the nullable case.

(...And I say this as a Rust fan myself, for what it's worth.)

Re:Null References: The Billion Dollar Mistake by amelius on Saturday January 11, 2020 @09:28AM (#22020029)
Imho, Rust is an awkward language because it positions itself as a systems language but it makes low-level stuff more difficult (there's even a book teaching how to implement doubly linked lists in Rust [1]), hence prone to mistakes. At the same time, people are using Rust to build non-systems programs, where other languages would be more appropriate (e.g. those with garbage collectors). I don't think it is a good idea that Rust is promoted as the language that will rule them all; in my opinion, it is still a research language.

Linus Torvalds said the following about Rust [2]:

[What do you think of the projects currently underway to develop OS kernels in languages like Rust (touted for having built-in safeties that C does not)?]

> That's not a new phenomenon at all. We've had the system people who used Modula-2 or Ada, and I have to say Rust looks a lot better than either of those two disasters.

> I'm not convinced about Rust for an OS kernel (there's a lot more to system programming than the kernel, though), but at the same time there is no question that C has a lot of limitations.

[1] https://rust-unofficial.github.io/too-many-lists/

[2] https://www.infoworld.com/article/3109150/linux-at-25-linus-...

Re:Null References: The Billion Dollar Mistake by zozbot234 on Saturday January 11, 2020 @09:47AM (#22020117)
> there's even a book teaching how to implement doubly linked lists in Rust [1]

Doubly-linked lists are an awkward example because the "safety" of a doubly-linked list as a data structure involves fairly complex invariants that Rust can't even keep track of at this point, much less check independently. These things are exactly why the unsafe{} escape-hatch exists and is actively supported. But just looking at the amount of unsafe code in common Rust projects should suffice to figure out that this is not the common case, at all.

> At the same time, people are using Rust to build non-systems programs, where other languages would be more appropriate (e.g. those with garbage collectors).

Garbage collectors are good for one thing, and one thing only: keeping track of complex, spaghetti-like reference graphs where cycles, etc. can arise, perhaps even as a side effect of, say, implementing some concurrency-related pattern. Everything else is most likely better dealt with by a Rust-like system with optional support for reference counted data.

That's without even mentioning the other advantages that a Rust-like ownership system provides over a GC-only language. See e.g. https://llogiq.github.io/2020/01/10/rustvsgc.html this recent post for some nice examples.

Re:Null References: The Billion Dollar Mistake by amelius on Saturday January 11, 2020 @10:55PM (#22024352)
> Doubly-linked lists are an awkward example because the "safety" of a doubly-linked list as a data structure involves fairly complex invariants that Rust can't even keep track of at this point.

Perhaps it's just me, but I'd like to assume that my language does not treat any algorithm found in a basic algorithms course (e.g. Sedgewick) as awkward.

Re:Null References: The Billion Dollar Mistake by jayd16 on Saturday January 11, 2020 @11:18PM (#22024447)
Eh, I have no problem with the idea that data structures that work in C are awkward in different paradigms. Many data structures are awkward in functional programming. Lots of things are awkward in C that are easier in other languages.
Re:Null References: The Billion Dollar Mistake by elcritch on Sunday January 12, 2020 @08:15AM (#22026152)
How times have you used doubly liked lists outside a CS101 programming exercise? Even if you did, it’d usually be trivial to implement an array index version or just use unsafe. Basically it seems you give up almost nothing for memory safety.
Re:Null References: The Billion Dollar Mistake by dpbriggs on Saturday January 11, 2020 @11:22PM (#22024462)
For sure, you can avoid awkwardness by not statically verifying memory usage & invariants (c++, etc) or using a GC'd language. Rust's ownership and borrowing rules are limited, but simple enough for someone to internalize them quickly.

There's a pretty vast difference between human simple and computer simple. Rust requires that you prove memory safety, or use unsafe. That's a different problem than just informally ensuring invariants are met.

You could probably pull in more advanced type theory research for more nuanced ownership, but I'd bet the language would be harder to understand overall (Haskell disease).

Re:Null References: The Billion Dollar Mistake by empath75 on Saturday January 11, 2020 @06:36PM (#22023232)
I’m a rust fan, but garbage collection is about removing having to think about memory management from the developer almost entirely, not about performance.
Re:Null References: The Billion Dollar Mistake by perl4ever on Wednesday January 15, 2020 @12:58AM (#22052056)
I don't do the sort of programming recently where it matters, but when I read the debates on GC on HN, I think, why not a language where there is a GC, but it is "cooperatively scheduled" - you explicitly invoke it for a fixed amount of time. Wouldn't that be the best of both worlds?
Re:Null References: The Billion Dollar Mistake by bitwize on Saturday January 11, 2020 @06:43PM (#22023269)
Value and move semantics do the same thing and work in 90% of the cases a GC does. For the remainder there's ARC.
Re:Null References: The Billion Dollar Mistake by empath75 on Saturday January 11, 2020 @07:09PM (#22023392)
If you think rust eliminates the need to think about it in the way that python does, I don’t know what to tell you.
Re:Null References: The Billion Dollar Mistake by bluejekyll on Saturday January 11, 2020 @09:43PM (#22024114)
You might be reading too much into that comment.

Python allows you to do something with memory that Rust has made it a priority to be more concerned with, and that’s sharing.

C also has easily shared memory, much like Python. Point being that Rust wants to make sure that your references are safe to share, whereas Python wants you to share as much as possible and makes it safe by not allowing multiple threads to interact with it.

These are different trade offs, but Rust does allow you to forget about memory management in the same way Python does, but forces you to think how it’s being shared.

That’s the added cost over Python and the extra thought that goes into using the language.

Re:Null References: The Billion Dollar Mistake by tsimionescu on Sunday January 12, 2020 @03:45AM (#22025226)
No, having to think about value and move semantics is extra overhead you take on. It's better when the compiler can help you catch this, like in Rust, but it still forces you to structure your program a certain way and to constantly think about incidental details like ownership.
Re:Null References: The Billion Dollar Mistake by jayd16 on Saturday January 11, 2020 @11:13PM (#22024427)
GCs are also good at compacting heaps.
Re:Null References: The Billion Dollar Mistake by zozbot234 on Saturday January 11, 2020 @11:30PM (#22024484)
Perhaps, but at pretty severe cost. Your heap must be structured in a way that the tracing routine can make sense of (and the consequences of this involve considerable waste and inefficiency in practice - lots and lots of gratuitous pointer chasing), and the compacting step itself involves a lot of traffic on the memory bus that trashes your caches and hogs precious memory bandwidth.

Forget it. Obligate GC is a terrible idea unless you really, really, really know what you're doing.

Re:Null References: The Billion Dollar Mistake by mhh__ on Saturday January 11, 2020 @06:27PM (#22023171)
Rust is a force for good but I think Andrei Alexandrescu was right when he said Rust feels like it "skipped leg day" (in the sense that it has its party piece and not much else) - from the perspective of the arch metaprogrammer himself at least.

Rust is obviously good for safety but for everything else (to me at least) it seems unidiomatic and ugly, admittedlty I've never really sunk my teeth into it (I've read a fair amount into the theory behind the safety features but never done a proper project)

Re:Null References: The Billion Dollar Mistake by Stratoscope on Saturday January 11, 2020 @10:47PM (#22024329)
> ...Andrei Alexandrescu was right when he said Rust feels like it "skipped leg day" (in the sense that it has its party piece and not much else) - from the perspective of the arch metaprogrammer himself at least.

Mind if I ask what that means? It seems like an interesting observation, but there are a couple of bits of terminology I don’t understand, like "leg day" and "party piece".

Any clarification would be appreciated, thanks!

Re:Null References: The Billion Dollar Mistake by amelius on Saturday January 11, 2020 @11:04PM (#22024394)
Perhaps this link would explain it more clearly: [1].

Leg-day is bodybuilding terminology, and refers to the day of the week when the bodybuilder is supposed to be training the leg muscles. According to the meme, nobody wants to train the legs because they show the least.

[1] https://www.jonathanturner.org/2016/01/rust-and-blub-paradox...

Re:Null References: The Billion Dollar Mistake by Stratoscope on Saturday January 11, 2020 @11:15PM (#22024439)
Thank you, that is very informative!

> nobody wants to train the legs because they show the least.

It reminds me of the story about how Google drops products because no one wants to maintain an existing product. That would not show "impact", not like launching a new product would, and impact is how you get raises and promotions.

Re:Null References: The Billion Dollar Mistake by zozbot234 on Saturday January 11, 2020 @08:43PM (#22023856)
Rust has procedural macros. What else do you need for metaprogramming?
Re:Null References: The Billion Dollar Mistake by heavenlyblue on Saturday January 11, 2020 @08:10PM (#22023686)
>> unidiomatic and ugly

I am not sure I would take anyone seriously who thinks this is a valid point to make about a programming language.

Re:Null References: The Billion Dollar Mistake by mhh__ on Saturday January 11, 2020 @10:12PM (#22024218)
That was my personal opinion, unrelated to the first paragraph.
Re:Null References: The Billion Dollar Mistake by samatman on Saturday January 11, 2020 @05:33PM (#22022843)
This comment would be much improved with a list of those languages.

Kotlin and Swift come to mind, what are others?

Re:Null References: The Billion Dollar Mistake by kragen on Saturday January 11, 2020 @05:41PM (#22022890)
If we're limiting ourselves only to new languages, then nulls are statically excluded not only by Kotlin and Apple’s imitation of it, Swift, but also by F#, Agda, Idris, Elm, and (sort of) Scala. But the zozbot didn't seem to be talking only about new languages, so Haskell, Miranda, Clean, ML, SML, Caml, Caml-Light, and OCaml are also fair game. (It wouldn't be hard to list another dozen in that vein.) Moreover I think you could sort of make a case for languages like Prolog and Aardappel where you don't have a static type system at all, much less one that could potentially rule out nils, but in which the consequences of an unexpected nil can be much less severe than in traditional imperative and functional languages like Java, Lua, Python, Clojure, Smalltalk, or Erlang, which more or less need to crash or viralize the nil in those cases.
Re:Null References: The Billion Dollar Mistake by samatman on Saturday January 11, 2020 @06:08PM (#22023053)
Good list.

I've found the consequences of a nil type are less severe in dynamic languages, where all variables have the Any type, since nil is just one of the options one needs to account for.

Static languages where everything is nullable are reneging on the promise; you say something is a String but that just means Option<String>, and it saps a lot of the reasoning power which static typing should give.

Re:Null References: The Billion Dollar Mistake by philwelch on Saturday January 11, 2020 @10:21PM (#22024246)
Even in dynamic languages, the consequences can be pretty bad. For example, I've seen lots of Ruby bugs where things end up being unexpectedly `nil`, but I haven't seen as many Python bugs where things end up being unexpectedly `None`.

How does this happen? Well, in Ruby it's a lot more normal to just return nil. For example, consider the following code snippet:

  [][0]
In both languages you are trying to examine the zeroth element of an empty array (or list, as Python calls it). In Ruby this evaluates to nil. Python throws an IndexError. So in Python, if you have a bug where you address an array with an invalid index, it manifests as an error in how you're indexing the list. Ruby silently returns nil, and the only actual error backtraces you see are when you actually try to call a method on this nil later on, which might not be anywhere near where your program messed up the array indexing.
Re:Null References: The Billion Dollar Mistake by perl4ever on Wednesday January 15, 2020 @01:02AM (#22052066)
That seems (although I don't have experience with either language) straightforwardly correct treatment in Python and wrong in Ruby, and the problem seemingly should be attributed to [lack of] range checking, not nil/null.
Re:Null References: The Billion Dollar Mistake by philwelch on Wednesday January 15, 2020 @01:47PM (#22057098)
Sure; in this case, range checking provides an alternative behavior to generating a null reference. But Ruby has other places where it generates null references more promiscuously than Python. Java does too. If you take every use case that could generate a null reference and instead behave differently in that situation you’ve eliminated null references, and Python has largely done so despite having a None type.
Re:Null References: The Billion Dollar Mistake by nielsbot on Saturday January 11, 2020 @07:09PM (#22023393)
`let s:String` is not the same as `let s:String?` in Swift, at least. (Nor in TypeScript)
Re:Null References: The Billion Dollar Mistake by kragen on Saturday January 11, 2020 @08:16PM (#22023727)
Right, that's why Swift is in the lists above. TypeScript would've been a good addition, I just didn't think of it.
Re:Null References: The Billion Dollar Mistake by kragen on Saturday January 11, 2020 @06:17PM (#22023112)
Concur.
Re:Null References: The Billion Dollar Mistake by _bxg1 on Sunday January 12, 2020 @12:44AM (#22024699)
TypeScript/Flow
Re:Null References: The Billion Dollar Mistake by wvenable on Saturday January 11, 2020 @09:02PM (#22023935)
PHP 7