5 ms·
let foo = "..."; let foo = parse(foo); let foo = escaped(foo); Is it really shadowing or mutation of foo? I would consider this shadowing (something you c
by StreamBright 8y ago
let foo = "...";
let foo = parse(foo);
let foo = escaped(foo);
Is it really shadowing or mutation of foo?
I would consider this shadowing (something you can do in OcaML):
let foo = "..." in
let foo = parse(foo) in
let foo = escaped(foo) in
dosomething(foo);;
- UK-Al05 8y agoYou can't mutate variables in rust without declaring mut
- StreamBright 8y agoBut how do you know just by reading that code? It would not be obvious to me at all.
- goto11 8y agoYou mean if you don't know the language?
- StreamBright 8y agoI guess I just need to learn Rust. I wouldn't write a style of code where I have 3 lets after an other and using the same variable name.
- bryal 8y agoI think it's quite fine to assume the reader knows enough rust to know the difference between mutable and immutable variables.
- empath75 8y agolet foo = bar; let foo = bat; is shadowing let foo = bar; foo = bat; is a compilation-time error because foo isn't mutable. let mut foo = bar; foo = bat; is reassignment. in working code, either foo is declared as mutable or it's not, and it's pretty obvious from the code what's happening.
- StreamBright 8y agoI guess. I would just never write code like that.
- deleted 8y ago[deleted]
- hamaluik 8y agoI guess if you don't know rust you might not? But one of the first things you learn about rust is the `mut` keyword.
- eximius 8y ago`let` rebinds. If there isn't a `let`, it's mutation.
- hajile 8y agoIt is shadowing (though it could be converted into mutation by a sufficiently smart compiler...). ReasonML does something similar with their take on Ocaml. https://reasonml.github.io/docs/en/let-binding https://reasonml.github.io/docs/en/let-binding
- qznc 8y agoThe foos can all have different types which is not possible with mutation.
- int_19h 8y agoLook at it closely - it's exactly the same as your OCaml example, except it uses ";" instead of "in", and indentation is adjusted accordingly. Although in practice I found that it's common to not indent adjacent nested let..in blocks in OCaml, either, so you'd often see something like: let foo = "..." in let foo = parse(foo) in let foo = escaped(foo) in ... And the rules are really simple - "let" always introduces a new binding.
- IshKebab 8y agoIt's really shadowing. The type can change and it isn't declared mutable.