> If we look to a language like Haskell we see that reassigning variables, in general, is frowned upon (is it even possible?) This is perfectly valid Haskell: main = do let a = 7 print a let a = 9 print a but you'll get a warning for it: tmp.hs:4:7: warning: This binding for ‘a’ shadows the existing binding bound at tmp.hs:2:7
Should function arguments be reassignable or mutable?
11–20 of 28 posts
Re: Should function arguments be reassignable or mutable?
#12I think immutability of arguments should be optional by using a const keyword, like in c: int add(const int a, const int b);
Re: Should function arguments be reassignable or mutable?
#13I feel the author isn't going far enough- make all variables immutable by default unless explicitly declared otherwise, and even then highly discouraged. Where mutable variables are needed, I prefer to hide that logic inside a method that exists solely to do that dirty business on an abstract level, not specific to my business logic. That let's me test the 'dirty' mutable method in an isolated place and removes a who…
Agreed. Erlang is another language with single assignment, i.e. every variable assignment target must have a new name. It's incredibly easy to see what calculations and conditions affect the final outcome.
Re: Should function arguments be reassignable or mutable?
#14> If we look to a language like Haskell we see that reassigning variables, in general, is frowned upon (is it even possible?) This is perfectly valid Haskell: main = do let a = 7 print a let a = 9 print a but you'll get a warning for it: tmp.hs:4:7: warning: This binding for ‘a’ shadows the existing binding bound at tmp.hs:2:7
Why would they allow this? Is there any legitimate case where doing this makes sense? Seems very risky to me. In most languages I know you would expect that the "a" variables are the same.
fn foo(a):
a = a |> trim |> (default "chocolate")
mix a "milk"
Well, in this contrived example, I wouldn't reassign a, just tack on `|> (mix "milk")` but in a complex example where you're reusing `a` in several places, it can make sense and is nicer than having to declare a bunch of variables just to hold the sanitized values of arguments.Re: Should function arguments be reassignable or mutable?
#15Earlier quoted context omitted.
Agreed. Erlang is another language with single assignment, i.e. every variable assignment target must have a new name. It's incredibly easy to see what calculations and conditions affect the final outcome.
How do you do a for loop in Erlang or something that sums up a value ?
Re: Should function arguments be reassignable or mutable?
#16Earlier quoted context omitted.
Agreed. Erlang is another language with single assignment, i.e. every variable assignment target must have a new name. It's incredibly easy to see what calculations and conditions affect the final outcome.
How do you do a for loop in Erlang or something that sums up a value ?
Here is an example of using a recursive function in Erlang to sum up a factorial.
fac(1) ->
1;
fac(N) ->
N * fac(N - 1).Re: Should function arguments be reassignable or mutable?
#17Re: Should function arguments be reassignable or mutable?
#18Earlier quoted context omitted.
How do you do a for loop in Erlang or something that sums up a value ?
You don't usually write for loops in Erlang, as a functional language loops are better expressed as recursive functions. Here is an example of using a recursive function in Erlang to sum up a factorial. fac(1) -> 1; fac(N) -> N * fac(N - 1).
Re: Should function arguments be reassignable or mutable?
#19Earlier quoted context omitted.
Why would they allow this? Is there any legitimate case where doing this makes sense? Seems very risky to me. In most languages I know you would expect that the "a" variables are the same.
In the past, I've used it in order to ensure the variable has a legitimate value. So, something like this (pseudo code): fn foo(a): a = a |> trim |> (default "chocolate") mix a "milk" Well, in this contrived example, I wouldn't reassign a, just tack on `|> (mix "milk")` but in a complex example where you're reusing `a` in several places, it can make sense and is nicer than having to declare a bunch of variables just…
Re: Should function arguments be reassignable or mutable?
#20Earlier quoted context omitted.
You don't usually write for loops in Erlang, as a functional language loops are better expressed as recursive functions. Here is an example of using a recursive function in Erlang to sum up a factorial. fac(1) -> 1; fac(N) -> N * fac(N - 1).
Does Erlang handle the stack better? In most languages this would blow up if N got big enough.