bartblast

bartblast

Creator of Hologram

Changes in the language since 1.6 or error in the book?

After having some experience with Elixir I decided to read “Programming Elixir 1.6” by Dave Thomas for the second time, this time very thoroughly. I noticed something strange for me on page 37 (chapter Variable Scope):

Elixir doesn’t really have blocks such as these, but it does have ways of grouping expressions together. The most common of these is the do block:

line_no = 50
if (line_no == 50) do
IO.puts “new-page\f”
line_no = 0
end

IO.puts line_no

However, Elixir thinks this is a risky way to write code. In particular, it’s easy to forget to initialize line_no outside the block, but to then rely on it having a value after the block. For that reason, you’ll see a warning:

$ elixir back_block.ex
warning: the variable “line_no” is unsafe as it has been set inside one of: case, cond, receive, if, and, or, &&, ||. Please explicitly return the variable value instead. Here’s an example:

case integer do
  1 → atom = :one
  2 → atom = :two
end

should be written as

atom =
case integer do
  1 → :one
  2 → :two
end

Unsafe variable found at:
T.ex:10

0

TL;DR: A variable is initialized in the outer scope of if, then inside the if block it’s rebinded to a new value and the binding propagates to the outer scope.

I was always under the impression that in such case a new inner scope is created and you can’t change the binding in the outer scoped, so the result would be 50, and not 0, as in the quoted book snippet.
Did something change in the language since 1.6 or am I missing something?

Most Liked

NobbZ

NobbZ

Yes, imperative assignment has been removed from the language in 1.8 or 1.7.

Where Next?

Popular in Chat/Questions Top

Iex.new
Hello!, I just started this week to discover Elixir. I’m a PHP-Programmer and did some sutff in Go too. The more I read about Elixir t...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
armanm
I know zero downtime deployment can mean different things depending on your application and what your users can tolerate so expectations ...
New
maz
I’m getting this error: ## ** (Ecto.Query.CompileError) Tuples can only be used in comparisons with literal tuples of the same size fro...
New
New
makeitrein
More Ecto questions! More madness! The context: there’s a list of books that I want to filter with a dropdown… The dropdown: looks some...
New
New
AstonJ
It finally feels like I’ve got some time to catch up on my reading - though I am sure lots of people will be wondering the same: what are...
New
younes-alouani
I'm studying Phoenix Framework but I want to understand basics first. Which book/mooc explains better the Design of Network-based Softwar...
New
Fl4m3Ph03n1x
Background I am trying to do the typical Ceaser cypher exercise in Elixir. The description of the exercise is as follows: Create an impl...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement