为什么在这个 Elixir 程序中需要 :error 模式匹配?

Non*_*ona 1 elixir

我只是想从 Programming Elixir 1.0 中运行一个示例 Elixir 程序并得到以下错误,尽管Supervisor 文档使我看起来不应该这样做:

我究竟做错了什么?

我执行iex -S mix并查看错误报告:

Erlang/OTP 18 [erts-7.2] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]

=INFO REPORT==== 2-Apr-2016::20:11:46 ===
    application: logger
    exited: stopped
    type: temporary
** (Mix) Could not start application sequence_supervisor: exited in: SequenceSupervisor.start(:normal, [])
    ** (EXIT) an exception was raised:
        ** (MatchError) no match of right hand side value: {:error, {:shutdown, {:failed_to_start_child, Sequence.Server, {:EXIT, {:undef, [{Sequence.Server, :start_link, '{', []}, {:supervisor, :do_start_child, 2, [file: 'supervisor.erl', line: 343]}, {:supervisor, :start_children, 3, [file: 'supervisor.erl', line: 326]}, {:supervisor, :init_children, 2, [file: 'supervisor.erl', line: 292]}, {:gen_server, :init_it, 6, [file: 'gen_server.erl', line: 328]}, {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 240]}]}}}}}
            (sequence_supervisor) lib/sequence_supervisor.ex:18: SequenceSupervisor.start/2
            (kernel) application_master.erl:273: :application_master.start_it_old/4
defmodule SequenceSupervisor do
  use Application

  # See http://elixir-lang.org/docs/stable/elixir/Application.html
  # for more information on OTP Applications
  def start(_type, _args) do
    import Supervisor.Spec, warn: false

    children = [
      # Define workers and child supervisors to be supervised
      # worker(SequenceSupervisor.Worker, [arg1, arg2, arg3]),
      worker(SequenceSupervisor.Server, [123])
    ]

    # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: SequenceSupervisor.Supervisor]
    {:ok, _pid} = Supervisor.start_link(children, opts)
  end
end
Run Code Online (Sandbox Code Playgroud)

目录树

.
??? _build
?   ??? dev
?       ??? lib
?           ??? sequence_supervisor
?               ??? ebin
?                   ??? Elixir.SequenceSupervisor.beam
?                   ??? sequence_supervisor.app
??? config
?   ??? config.exs
??? lib
?   ??? sequence_supervisor.ex
??? mix.exs
??? README.md
??? test
    ??? sequence_supervisor_test.exs
    ??? test_helper.exs
Run Code Online (Sandbox Code Playgroud)

sequence_supervisor.ex

defmodule Sequence do
  use Application

  def start(_type, _args) do
    import Supervisor.Spec, warn: false

    children = [
      worker(Sequence.Server, [123])
    ]

    opts = [strategy: :one_for_one, name: Sequence.Supervisor]
    {:ok, _pid} = Supervisor.start_link(children, opts)
  end
end
Run Code Online (Sandbox Code Playgroud)

混合文件(mix.exs)

defmodule SequenceSupervisor.Mixfile do
  use Mix.Project

  def project do
    [app: :sequence_supervisor,
     version: "0.0.1",
     elixir: "~> 1.1",
     build_embedded: Mix.env == :prod,
     start_permanent: Mix.env == :prod,
     deps: deps]
  end

  # Configuration for the OTP application
  #
  # Type "mix help compile.app" for more information
  def application do
    [applications: [:logger],
     mod: {SequenceSupervisor, []}]
  end

  # Dependencies can be Hex packages:
  #
  #   {:mydep, "~> 0.3.0"}
  #
  # Or git/path repositories:
  #
  #   {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"}
  #
  # Type "mix help deps" for more examples and options
  defp deps do
    []
  end
end
Run Code Online (Sandbox Code Playgroud)

Cod*_*oll 5

如果您查看提供的堆栈跟踪,则有一个段:

{:undef, [{Sequence.Server, :start_link, '{', []
Run Code Online (Sandbox Code Playgroud)

这意味着有什么东西试图调用start_linkSequence.Server模块上的函数,但它是一个未定义的函数。

从您的目录树来看,不仅start_link函数Sequence.Server未定义,而且Sequence.Server模块本身看起来也未定义。这可能是因为您还没有为它编写代码。

它的代码可以在Programming Phoenix 配套站点上找到