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 Like

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.

Fantastic! Thanks a ton, @Margaret, and I hope it helps.

I’m thoroughly enjoying the book, BTW, now that I’m, finally getting around to reading it.