bradleyscollins

bradleyscollins

Programming Elixir 1.6 - Inaccurate code in "More on Application Parameters" section (page 281)

The code here does not follow from previous sections, making it difficult to identify the changes Mr. Thomas intends to introduce under the heading More on Application Parameters.

In the previous section, the application function in mix.exs was as follows:

# p. 280
def application do
  [
    mod: {
      Sequence.Application, 456
    },
    registered: [
      Sequence.Server,
    ],
    extra_applications: [:logger], 
  ]
end

But on p. 281, we not only lost the extra_applications line, but the main entry point on the mod line has also changed.

# p. 281
def application do
  [
    mod:        { Sequence, [] }, # Why not `Sequence.Application`?
    env:        [initial_number: 456],
    registered: [ Sequence.Server ]
    # Where did `extra_applications: [:logger],` go?
  ]
end

It looks as if the correct code should be the following:

# p. 281 (corrected)
def application do
  [
    mod:                { Sequence.Application, [] },
    env:                [ initial_number: 456 ],
    registered:         [ Sequence.Server ],
    extra_applications: [ :logger ],
  ]
end

This leads me to believe that there is also a typo back on p. 279 in the last paragraph:

For the sequence app, we tell OTP that the Sequence Sequence.Application module is the main entry point. OTP will call this module’s start function when it starts the application. The second element of the tuple is the parameter to pass to this function. In our case, it’s the initial number for the sequence.

Finally, when last we saw the contents of the application.ex file on p. 279, it looked like this:

# p. 279
defmodule Sequence.Application do
  @moduledoc false

  use Application

  def start(_type, initial_number) do
    children = [
      { Sequence.Stash,  initial_number},
      { Sequence.Server, nil},
    ]

    opts = [strategy: :rest_for_one, name: Sequence.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

But the proposed change on p. 281 makes it look as if we’re in a totally different file!

  • The module name is different, and
  • The start function is missing some key information.
# p. 281
defmodule Sequence do # Why not `Sequence.Application`?
  use Application

  def start(_type, _args) do
    # Where did the `children` lines and the `opts` line go?
    Sequence.Supervisor.start_link(Application.get_env(:sequence, :initial_number))
  end
end

In fact, I had originally thought (because of the module name difference) that Mr. Thomas intends for us to put this code in lib/sequence.ex rather than lib/sequence/sequence.ex. :man_facepalming:

So that it is easier to see where this code needs to go and what changes to make, I suggest updating the second code block on p. 281 to the following, which appears to be what Mr. Thomas intends:

# p. 281 (corrected)
defmodule Sequence.Application do
  @moduledoc false

  use Application

  def start(_type, _args) do                                        # <--
    initial_number = Application.get_env(:sequence, :initial_number) # <--

    children = [
      { Sequence.Stash,  initial_number},
      { Sequence.Server, nil},
    ]

    opts = [strategy: :rest_for_one, name: Sequence.Supervisor]
    Supervisor.start_link(children, opts)
  end
end
1 762 2

First Post!

Margaret

Margaret

Editor at PragProg

Hi @bradleyscollins, thank you for submitting the errata and comments for Programming Elixir 1.6 #book-programming-elixir-1-6. We’ll be sure to note that changes may be needed in the next printing.

Where Next?

Popular Pragmatic Bookshelf topics Top

telemachus
Python Testing With Pytest - Chapter 2, warnings for “unregistered custom marks” While running the smoke tests in Chapter 2, I get these...
5 2075 1
New
raul
Page 28: It implements io.ReaderAt on the store type. Sorry if it’s a dumb question but was the io.ReaderAt supposed to be io.ReadAt? ...
0 1297 3
New
joepstender
The generated iex result below should list products instead of product for the metadata. (page 67) iex&gt; product = %Product{} %Pento....
0 1798 20
New
New
jskubick
I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
2 1164 8
New
jskubick
I found an issue in Chapter 7 regarding android:backgroundTint vs app:backgroundTint. How to replicate: load chapter-7 from zipfile i...
0 3500 3
New
mert
AWDWR 7, page 152, page 153: Hello everyone, I’m a little bit lost on the hotwire part. I didn’t fully understand it. On page 152 @rub...
0 1225 4
New
Keton
When running the program in chapter 8, “Implementing Combat”, the printout Health before attack was never printed so I assumed something ...
2 1019 2
New
a.zampa
@mfazio23 I’m following the indications of the book and arriver ad chapter 10, but the app cannot be compiled due to an error in the Bas...
1 3360 6
New
mcpierce
@mfazio23 I’ve applied the changes from Chapter 5 of the book and everything builds correctly and runs. But, when I try to start a game,...
0 1160 10
New

Other popular topics Top

AstonJ
If it’s a mechanical keyboard, which switches do you have? Would you recommend it? Why? What will your next keyboard be? Pics always w...
144 8502 50
New
AstonJ
This looks like a stunning keycap set :orange_heart: A LEGENDARY KEYBOARD LIVES ON When you bought an Apple Macintosh computer in the e...
14 6073 7
New
Margaret
Hello content creators! Happy new year. What tech topics do you think will be the focus of 2021? My vote for one topic is ethics in tech...
110 3900 43
New
DevotionGeo
The V Programming Language Simple language for building maintainable programs V is already mentioned couple of times in the forum, but I...
21 11128 7
New
PragmaticBookshelf
Rails 7 completely redefines what it means to produce fantastic user experiences and provides a way to achieve all the benefits of single...
32 3916 9
New
PragmaticBookshelf
Author Spotlight Jamis Buck @jamis This month, we have the pleasure of spotlighting author Jamis Buck, who has written Mazes for Prog...
21 5598 9
New
Help
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
8 3528 3
New
PragmaticBookshelf
Author Spotlight: Bruce Tate @redrapids Programming languages always emerge out of need, and if that’s not always true, they’re defin...
54 4591 23
New
PragmaticBookshelf
Develop, deploy, and debug BEAM applications using BEAMOps: a new paradigm that focuses on scalability, fault tolerance, and owning each ...
40 2292 21
New
AnfaengerAlex
Hello, I’m a beginner in Android development and I’m facing an issue with my project setup. In my build.gradle.kts file, I have the foll...
0 2183 2
New

Sub Categories: