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

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

abtin
page 20: … protoc command… I had to additionally run the following go get commands in order to be able to compile protobuf code using go...
New
johnp
Hi Brian, Looks like the api for tinydb has changed a little. Noticed while working on chapter 7 that the .purge() call to the db throws...
New
conradwt
First, the code resources: Page 237: rumbl_umbrella/apps/rumbl/mix.exs Note: That this file is missing. Page 238: rumbl_umbrella/app...
New
HarryDeveloper
Hi @venkats, It has been mentioned in the description of ‘Supervisory Job’ title that 2 things as mentioned below result in the same eff...
New
New
AndyDavis3416
@noelrappin Running the webpack dev server, I receive the following warning: ERROR in tsconfig.json TS18003: No inputs were found in c...
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
jwandekoken
Book: Programming Phoenix LiveView, page 142 (157/378), file lib/pento_web/live/product_live/form_component.ex, in the function below: d...
New
andreheijstek
After running /bin/setup, the first error was: The foreman' command exists in these Ruby versions: That was easy to fix: gem install fore...
New
bjnord
Hello @herbert ! Trying to get the very first “Hello, Bracket Terminal!" example to run (p. 53). I develop on an Amazon EC2 instance runn...
New

Other popular topics Top

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
DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
PragmaticBookshelf
“A Mystical Experience” Hero’s Journey with Paolo Perrotta @nusco Ever wonder how authoring books compares to writing articles?...
New
AstonJ
Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New
AstonJ
Biggest jackpot ever apparently! :upside_down_face: I don’t (usually) gamble/play the lottery, but working on a program to predict the...
New
PragmaticBookshelf
Author Spotlight James Stanier @jstanier James Stanier, author of Effective Remote Work , discusses how to rethink the office as we e...
New
New
New
sir.laksmana_wenk
I’m able to do the “artistic” part of game-development; character designing/modeling, music, environment modeling, etc. However, I don’t...
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...
New

Sub Categories: