Elixir出错:返回错误值:: ok

时间:2017-11-12 08:56:44

标签: elixir mix

在Elixir上工作时遇到了一些问题。 这是我的mix.exs ...

defmodule FlockTest.Mixfile do
  use Mix.Project

  def project do
    [app: :flock_test,
     version: "0.1.0",
     elixir: "~> 1.4",
     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
    # Specify extra applications you'll use from Erlang/Elixir
    [
      extra_applications: [:logger],
      mod: {FlockTest, []}
    ]
  end
...
...

这是FlockTest的代码。

defmodule FlockTest do
  @moduledoc """
  Documentation for FlockTest.
  """

  def start(_type, _args) do
    IO.puts "Start Flock"
  end
end

我使用mix run --no-halt运行此代码,但这会导致这样的错误。

=INFO REPORT==== 12-Nov-2017::17:47:39 ===
    application: logger
    exited: stopped
    type: temporary
** (Mix) Could not start application flock_test: FlockTest.start(:normal,     
[]) returned a bad value: :ok

我做错了吗?

1 个答案:

答案 0 :(得分:7)

您的应用程序的start/2函数应该启动并返回应用程序的顶级进程,通常是一个超级用户实例。现在,您返回IO.puts返回的值,成功时为:ok。有效的返回值为{:ok, pid} | {:ok, pid, state} | {:error, reason :: term},如文档here所示。有关此示例,您可以使用mix new foo --sup创建新应用程序并查看lib/foo/application.ex。以下是start/2的外观:

def start(_type, _args) do
  # List all child processes to be supervised
  children = [
    # Starts a worker by calling: A.Worker.start_link(arg)
    # {A.Worker, arg},
  ]

  # See https://hexdocs.pm/elixir/Supervisor.html
  # for other strategies and supported options
  opts = [strategy: :one_for_one, name: A.Supervisor]
  Supervisor.start_link(children, opts)
end