为什么我需要这个Elixir程序中的错误模式匹配?

时间:2016-04-03 00:18:20

标签: elixir

我只是试图从编程Elixir 1.0运行一个Elixir样本程序并得到以下错误,虽然文档使我觉得我不应该(http://elixir-lang.org/docs/stable/elixir/Supervisor.html):

我做错了什么?

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

目录树

.
├── _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

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

混合文件(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

1 个答案:

答案 0 :(得分:4)

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

{:undef, [{Sequence.Server, :start_link, '{', []

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

根据您的目录树判断,start_link的{​​{1}}函数不仅未定义,但看起来Sequence.Server模块本身未定义。这可能是因为你还没有为它编写代码。

可以在Programming Phoenix companion site上找到它的代码。