5 ms·
Ironically the "simple" JS program has a bug in it. The documentation for fs.watch is very explicit that the filename in the callback can be null and that you n
by khuey 1y ago
Ironically the "simple" JS program has a bug in it. The documentation for fs.watch is very explicit that the filename in the callback can be null and that you need to check for that. In Rust that fact would be encoded in the type system and the programmer would be forced to handle it, but in JS it's easier to just write bad code.
https://nodejs.org/api/fs.html#filename-argument https://nodejs.org/api/fs.html#filename-argument
- sestep 1y agoTrue, but also the loop syntax is simply incorrect, which would have been caught by running it; so probably a better interpretation is simply that the author didn't spend much time thinking about that JavaScript code because it didn't matter for their point.
- masklinn 1y ago> it didn't matter for their point. Which is mostly a lie, since at least half the concepts they gripe about in rust are in the js snippet.
- sestep 1y agoYep fair. I'm not sure why they didn't spend more effort on the JS part, just saying they didn't.
- serial_dev 1y agoBut isn’t it at least part of the problem? The JavaScript code looks completely fine, until the HN comment section finds various subtle bugs that sometimes not even TS can protect you against?
- jynelson 1y agono. the JS program has obvious syntax bugs that TS wouldn't compile. commentors above are right that I was just lazy (ty for the callout, I have since fixed the JS program).
- Waterluvian 1y agoTypescript would require you to check for null before use, which I think makes this a good example of how TS is oftentimes a fairly gentle step up from JS. Or, at least, closer to the correctness of Rust without all the heavy stuff.
- quotemstr 1y agoBut you don't get manual memory management then. No GC, safety, and simplicity: pick any two
- Waterluvian 1y agoThat’s true, two different tools have different pros and cons.
- ori_b 1y agoSure, but GC's not the only upside.
- TylerE 1y agoFor a large number of people including myself, manual memory management is a huge con, not a pro.
- ameliaquining 1y agoYes, but there are use cases where it's a hard requirement.
- kiitos 1y agoyep! but file watcher programs, or http services, or really anything that runs on modern linux servers, are not these use cases!
- koakuma-chan 1y agoDo you manually manage memory in Rust? Because I just write Vec::new or vec![], just like how I would write new ArrayList in Java.
- marcianx 1y agoThere are further bugs: `for path in paths` should be `for (const path of paths)` JS will immediately error on the lack of parens, but the `in` vs `of` iterates over indexes, not values, and those indexes are unfortunately converted to strings (since `for-in` is object field-name iteration). So even TypeScript would not have caught it when the (stringified) index is used as the first argument of `fs.watch()`.
- ptdorf 1y agoAm I blind or where is `kind` coming from? > console.log(`${kind} ${filename}`) It should be `eventType` (string).
- khuey 1y agoYes. It's coming from the Rust code (where the relevant property is named 'kind') because the author didn't actually run their JS snippet. They've (silently) fixed a number of issues people have pointed out (including the one I pointed out) but they still haven't noticed that one.