Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> Variable declarations can be overshadowed - this means that you can redeclare the existing variable with different data type in given scope if you need to.

I'm having a hard time deciding why I would want this. It seems more likely to result in bugs than being a useful feature.



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.


I 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.


Meh. 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


I 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 :)


It 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.


Haskell has got to be by far the least readable language in the world, all of that is incomprehensible


I'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.


Er, actually

  x = 2
  x = "foo"
  [y] = [3]
  [y] = ["bar"]
Sorry, I didn't re-read the code before translating.


It's useful in Rust where you might parse input as a string and then convert it to a number.

It's better than having a variable named age and another one named age_num or the opposite, str_age and age.


I guess that's fair. I haven't seen that pattern in Rust before and I don't think I particularly like it, but I can see why some might.


It's a pretty good way of preventing bugs actually. It prevents you from accidentally accessing the old value.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: