Rebinding variables in block expressions
mainElixir variables are immutable, but can be rebound. When using block expressions like if, case, or cond, you must bind the result of the entire expression to a variable if you want to use the updated value. You cannot rebind a variable inside the block and expect it to persist outside.
# INVALID: the rebind inside the block is lost
if connected?(socket) do
socket = assign(socket, :val, val)
end
# VALID: the result of the if expression is bound to socket
socket =
if connected?(socket) do
assign(socket, :val, val)
end