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 761 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.

Popular Prag Prog topics Top

belgoros
Following the steps described in Chapter 6 of the book, I’m stuck with running the migration as described on page 84: bundle exec sequel...
4 1149 1
New
herminiotorres
Hi @Margaret , On page VII the book tells us the example and snippets will be all using Elixir version 1.11 But on page 3 almost the en...
15 1542 13
New
herminiotorres
Hi! I know not the intentions behind this narrative when called, on page XI: mount() |&gt; handle_event() |&gt; render() but the correc...
3 1296 17
New
AleksandrKudashkin
On the page xv there is an instruction to run bin/setup from the main folder. I downloaded the source code today (12/03/21) and can’t see...
2 1425 11
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...
9 1256 4
New
jeremyhuiskamp
Title: Web Development with Clojure, Third Edition, vB17.0 (p9) The create table guestbook syntax suggested doesn’t seem to be accepted ...
0 1833 1
New
adamwoolhether
I’m not quite sure what’s going on here, but I’m unable to have to containers successfully complete the Readiness/Liveness checks. I’m im...
5 1393 5
New
AufHe
I’m a newbie to Rails 7 and have hit an issue with the bin/Dev script mentioned on pages 112-113. Iteration A1 - Seeing the list of prod...
0 2610 9
New
kolossal
Hi, I need some help, I’m new to rust and was learning through your book. but I got stuck at the last stage of distribution. Whenever I t...
3 1953 5
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...
7 1151 10
New

Other popular topics Top

ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
245 5109 101
New
AstonJ
Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face: Perhaps if there’s enough peop...
239 5577 90
New
Rainer
Not sure if following fits exactly this thread, or if we should have a hobby thread… For many years I’m designing and building model air...
200 3387 78
New
Exadra37
I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
66 20738 24
New
Exadra37
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
10 4951 6
New
mafinar
Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
155 4154 65
New
AstonJ
Saw this on TikTok of all places! :lol: Anyone heard of them before? Lite:
13 3728 4
New
Maartz
Hi folks, I don’t know if I saw this here but, here’s a new programming language, called Roc Reminds me a bit of Elm and thus Haskell. ...
49 4181 14
New
husaindevelop
Inside our android webview app, we are trying to paste the copied content from another app eg (notes) using navigator.clipboard.readtext ...
1 2912 0
New
PragmaticBookshelf
Author Spotlight: VM Brasseur @vmbrasseur We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
16 3834 11
New

Latest in PragProg

View all threads ❯