Posted by azhenley 5 days ago
Clippy offers lints for (three?) distinct ways of shadowing because in most cases it turns out people who don't like shadowing only had problems with typically one specific kind of shadowing (e.g. same type unrelated shadowing, or different type same value shadowing) and since that varies why not offer to diagnose the specific problem this programmer doesn't like.
To be concrete some people are worried about things like:
let sparrows = get_a_list_of_sparrows();
// ....
let sparrows = sparrows.len() + EXTRA_SPARROWS;
Whereas for some people that seems fine but they're worried about: let sparrows = find_wild_sparrows();
// ....
let sparrows = find_farmed_sparrows();
These are both shadowing the name sparrows, and Rust is fine with either but Clippy can provide different lints so that you can ban one of them while using the other freely in your codebase.HN is a site that's specifically designed to focus on the new and interesting aspects of topics.
Please don't pick the most provocative thing in an article or post to complain about in the thread. Find something interesting to respond to instead.
This has been in the guidelines for many years and it’s long been routine moderation that we ask people to avoid that kind of thing.
If it was just his ownership that would be one thing but his aggressive support for turning the site into /pol/ has ruined it.
> No, he's a competent programmer.
I don't think these are mutually exclusive
Variables are distinct from constants. It's a problem that C and C++ use the keyword "const" to signify immutability instead, indeed as a result C++ needed three more keywords "constexpr", "constinit" and "consteval" to try to grapple with the problem.
I want everything that passes through a function to be a copy unless I put in a symbol or keyword that it suppose to be passed by reference.
I made a little function to do deep copies but am still experimenting with it.
function deepCopy(value) {
if (typeof structuredClone === 'function') {
try { return structuredClone(value); } catch (_) {}
}
try {
return JSON.parse(JSON.stringify(value));
} catch (_) {
// Last fallback: return original (shallow)
return value;
}
}JavaScript doesn’t have references, it is clearer to only use “passed by reference” terminology when writing about code in a language which does have them, like C++ [0].
In JavaScript, if a mutable object is passed to a function, then the function can change the properties on the object, but it is always the same object. When an object is passed by reference, the function can replace the initial object with a completely different one, that isn’t possible in JS.
Better is to distinguish between immutable objects (ints, strings in JS) and mutable ones. A mutable object can be made immutable in JS using Object.freeze [1].
[0] https://en.wikipedia.org/wiki/Reference_(C%2B%2B)
[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
Maybe someone should work on a way to make references in javascript land. Sort of like immer but baked in.
I wish all arguments were copies unless I put some symbol that says, alright, go ahead and give me the original to mutate?
It seems like this way, you reduce side effects, and if you want the speed of just using the originals, you could still do that by using special notation.