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

Popular Prag Prog topics Top

jimmykiang
This test is broken right out of the box… — FAIL: TestAgent (7.82s) agent_test.go:77: Error Trace: agent_test.go:77 agent_test.go:...
New
Razor54672
The answer to 3rd Problem of Chapter 5 (Making Choices) of “Practical Programming, Third Edition” seems incorrect in the given answer ke...
New
brianokken
Many tasks_proj/tests directories exist in chapters 2, 3, 5 that have tests that use the custom markers smoke and get, which are not decl...
New
rmurray10127
Title: Intuitive Python: docker run… denied error (page 2) Attempted to run the docker command in both CLI and Powershell PS C:\Users\r...
New
New
brunogirin
When I run the coverage example to report on missing lines, I get: pytest --cov=cards --report=term-missing ch7 ERROR: usage: pytest [op...
New
creminology
Skimming ahead, much of the following is explained in Chapter 3, but new readers (like me!) will hit a roadblock in Chapter 2 with their ...
New
Henrai
Hi, I’m working on the Chapter 8 of the book. After I add add the point_offset, I’m still able to see acne: In the image above, I re...
New
gorkaio
root_layout: {PentoWeb.LayoutView, :root}, This results in the following following error: no “root” html template defined for PentoWeb...
New
davetron5000
Hello faithful readers! If you have tried to follow along in the book, you are asked to start up the dev environment via dx/build and ar...
New

Other popular topics Top

Devtalk
Hello Devtalk World! Please let us know a little about who you are and where you’re from :nerd_face:
New
PragmaticBookshelf
A PragProg Hero’s Journey with Brian P. Hogan @bphogan Have you ever worried that your only legacy will be in the form of legacy...
New
Exadra37
Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it. Does your monitor have eye p...
New
brentjanderson
Bought the Moonlander mechanical keyboard. Cherry Brown MX switches. Arms and wrists have been hurting enough that it’s time I did someth...
New
AstonJ
poll poll Be sure to check out @Dusty’s article posted here: An Introduction to Alternative Keyboard Layouts It’s one of the best write-...
New
PragmaticBookshelf
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
New
PragmaticBookshelf
“Finding the Boundaries” Hero’s Journey with Noel Rappin @noelrappin Even when you’re ultimately right about what the future ho...
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...
New
PragmaticBookshelf
Author Spotlight James Stanier @jstanier James Stanier, author of Effective Remote Work , discusses how to rethink the office as we e...
New
PragmaticBookshelf
Author Spotlight: Bruce Tate @redrapids Programming languages always emerge out of need, and if that’s not always true, they’re defin...
New

Latest in PragProg

View all threads ❯