4 ms·
It's a common practice in Rust at least, where instead of having a mutable variable which you modify across several lines, you declare a new immutable variable
by mort96 2y ago
It's a common practice in Rust at least, where instead of having a mutable variable which you modify across several lines, you declare a new immutable variable with the same name on those lines. I like it, but I guess it really just comes down to preference and what you're used to.
- mst 2y agoI quite like that pattern, but I think 'with a different datatype' should result in a small gnome climbing out the back of the computer and hitting the developer with a mallet.
- mort96 2y agoMeh. I don't mind: let thing = whatever(); let thing = thing.unwrap(); EDIT: what the hell I say I don't mind a particular pattern and that's enough for a downvote? Not that I care but I find this surprising
- michaelmior 2y agoI guess I haven't read enough Rust code to come across this patterns, but I don't think I particularly like it. Perhaps I would get used to it though :)
- kbp 2y agoIt works the same way in Haskell, eg main = do let x = 2 let x = "foo" y <- pure 3 y <- pure "bar" putStrLn $ x ++ y which is really the same as main = let x = 2 in let x = "foo" in pure 3 >>= \y -> pure "bar" >>= \y -> putStrLn $ x ++ y So it works pretty naturally where each assignment is kind of like a new scope. If the type system is good, I don't think it really causes issues.
- mort96 2y agoHaskell has got to be by far the least readable language in the world, all of that is incomprehensible
- kbp 2y agoI'm not sure what you find incomprehensible about the first example. The syntax is pretty standard. The only exotic thing is `$`, which is basically just like putting brackets around the rest of the line. Here's the first example roughly translated to Python: def main(): x = 2 [x] = ["foo"] y = 3 [y] = ["bar"] print(x + y) Seems about the same level of comprehensibility to me. Is there anything in particular you find difficult to understand? The second example is expanded out and not how a person would normally write it, but if you're familiar with the basic concepts it's using, it shows why it works very clearly; think of it like assembler.
- kbp 2y agoEr, actually x = 2 x = "foo" [y] = [3] [y] = ["bar"] Sorry, I didn't re-read the code before translating.