Elixir 十进制和 :erlang_decimal

Elixir Decimal and :erlang_decimal

我正在使用一个名为 erlang_decimal to do some decimal math. My application also uses Decimal 的 erlang 库作为 Ecto 的子依赖项,它是一个 Elixir 库,用于基本相同的事情。

这是我的 mix.exs:

  defp deps do
    [
      {:bcrypt_elixir, "~> 1.0"},
      {:comeonin, "~> 4.0"},
      {:configparser_ex, "~> 2.0"},
      {:csv, "~> 2.1.1"},
      {:decimal, "~> 1.0},
      {:ecto_enum, "~> 1.0"},
      {:ecto_sql, "~> 3.0"},
      {:erlang_decimal, "~> 0.4", app: false},
      {:ex_aws, "~> 2.0"},
      {:ex_aws_s3, "~> 2.0"},
      {:ex_machina, "~> 2.2", only: [:dev, :test]},
      {:ex_twilio, "~> 0.6.0"},
      {:gettext, "~> 0.11"},
      {:httpoison, "~> 1.0"},
      {:jason, "~> 1.0"},
      {:phoenix, "~> 1.4.0"},
      {:phoenix_ecto, "~> 4.0"},
      {:phoenix_html, "~> 2.10"},
      {:phoenix_live_reload, "~> 1.0", only: :dev},
      {:phoenix_pubsub, "~> 1.0"},
      {:plug, "~> 1.7"},
      {:plug_cowboy, "~> 2.0"},
      {:postgrex, ">= 0.0.0"},
      {:ranch, "~> 1.3"},
      {:sentry, "~> 7.0"},
      {:signed_overpunch, "~> 0.2"},
      {:sweet_xml, "~> 0.6"},
      {:telcom_parser, in_umbrella: true},
      {:timex, github: "bitwalker/timex", ref: "74afe810ee9c8e58e057830e62a865230ce00133"},
      {:trixie, in_umbrella: true}
    ]
  end

部署更改后,我 运行 出现以下异常:

  Elixir.UndefinedFunctionError: function Decimal.new/3 is undefined (module Decimal is not available)
    Module "Elixir.Decimal", in Decimal.new/3
    File "lib/postgrex/type_module.ex", line 713, in Postgrex.DefaultTypes."Elixir.Postgrex.Extensions.Numeric"/6
    File "lib/postgrex/protocol.ex", line 2733, in Postgrex.Protocol.rows_recv/4
    File "lib/postgrex/protocol.ex", line 1777, in Postgrex.Protocol.recv_execute/5
    File "lib/postgrex/protocol.ex", line 1652, in Postgrex.Protocol.bind_execute/4
    File "lib/db_connection/holder.ex", line 268, in DBConnection.Holder.holder_apply/4
    File "lib/db_connection.ex", line 1189, in DBConnection.run_execute/5
    File "lib/db_connection.ex", line 1276, in DBConnection.run/6
    Module "erlang", in :erlang.apply/2

是否存在某种命名空间冲突?

编辑:

Dialyzer 能够解决这个问题:

Total errors: 6, Skipped: 0
done in 0m5.83s
:0:unknown_function
Function Decimal.compare/2 does not exist.
________________________________________________________________________________
:0:unknown_function
Function Decimal.div/2 does not exist.
________________________________________________________________________________
:0:unknown_function
Function Decimal.mult/2 does not exist.
________________________________________________________________________________
:0:unknown_function
Function Decimal.new/1 does not exist.
________________________________________________________________________________
:0:unknown_function
Function Decimal.round/3 does not exist.
________________________________________________________________________________
:0:unknown_function
Function Decimal.to_integer/1 does not exist.
________________________________________________________________________________
done (warnings were emitted)

我更新了 mix.exs 以包含 {:decimal, "~> 1.0"}

根据您链接的 Decimal 文档:

Add Decimal as a dependency in your mix.exs file.

def deps do
  [{:decimal, "~> 1.0"}]
end

然而,您发布的 mix.exs 文件并未指定该依赖项。此外,错误消息说:

(module Decimal is not available)

这也表明您需要将 Decimal 模块添加到您的应用中。

==========

在努力使 erlang_decimal 工作之后,我尝试向 mix.exs 添加另一个依赖项:

 defp deps do
    [
      {:decimal, "~> 0.4.3", hex: :erlang_decimal},
      {:decimal, "~> 1.6"}

    ]

结果是这样的:

mix deps.get warning: the dependency :decimal is duplicated at the top level, please remove one of them Dependencies have diverged: * decimal (Hex package) different specs were given for the decimal app:

In mix.exs: {:decimal, "~> 0.4.3", [env: :prod, repo: "hexpm", hex: "erlang_decimal"]}

In mix.exs: {:decimal, "~> 1.6", [env: :prod, repo: "hexpm", hex: "decimal"]}

确保它们在您的部门中匹配或指定以上之一并设置 "override: true" **(混合)由于依赖项错误无法继续

接着看:

https://elixirforum.com/t/how-can-i-specify-multiple-dependencies-that-have-the-same-name/926

和:

https://elixirforum.com/t/working-with-modules-with-same-name/11364/6