简体   繁体   English

Elixir小数和:erlang_decimal

[英]Elixir Decimal and :erlang_decimal

I'm using an erlang library called erlang_decimal to do some decimal math. 我正在使用一个名为erlang_decimal的erlang库来进行一些十进制数学运算。 My application also uses Decimal as a sub-dependency for Ecto, which is an Elixir library for basically the same kind of thing. 我的应用程序还使用Decimal作为Ecto的子依赖关系,Ecto是一个Elixir库,用于处理基本上相同的事情。

Here's my mix.exs : 这是我的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

After deploying the change, I ran into the following exception: 部署更改后,我遇到了以下异常:

  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

Is there some sort of namespace conflict going on? 是否存在某种名称空间冲突?

Edit: 编辑:

Dialyzer is able to pick up on this issue: 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)

I updated the mix.exs to include {:decimal, "~> 1.0"} 我更新了mix.exs使其包含{:decimal, "~> 1.0"}

According to the Decimal docs that you linked: 根据您链接的十进制文档:

Add Decimal as a dependency in your mix.exs file. 在您的mix.exs文件中将Decimal添加为依赖项。

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

Yet, the mix.exs file you posted does not specify that dependency. 但是,您发布的mix.exs文件没有指定该依赖性。 In addition, the error message says: 此外,错误消息显示:

(module Decimal is not available) (模块十进制不可用)

which also indicates that you need to add the Decimal module to your app. 这还表明您需要将Decimal模块添加到您的应用中。

========== ==========

After struggling to get erlang_decimal to work, I tried adding another dependency to mix.exs: 在努力使erlang_decimal正常工作之后,我尝试向mix.exs添加另一个依赖项:

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

    ]

And this was the result: 结果是:

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: 混合deps.get警告:依赖项:decimal在顶层已重复,请删除其中之一。依赖项有所不同:*十进制(十六进制软件包)为十进制应用程序提供了不同的规格:

In mix.exs: {:decimal, "~> 0.4.3", [env: :prod, repo: "hexpm", hex: "erlang_decimal"]} 在mix.exs中:{:decimal,“〜> 0.4.3”,[env::prod,repo:“ hexpm”,十六进制:“ erlang_decimal”]}

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

Ensure they match or specify one of the above in your deps and set "override: true" ** (Mix) Can't continue due to errors on dependencies 确保它们匹配或在您的部门中指定上述之一,并设置为“ override:true”。**(混合)由于依赖项错误而无法继续

Then see: 然后看:

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

and: 和:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM