配置部分为空且非空

时间:2018-06-30 10:49:58

标签: elixir phoenix-framework

我有这个:

  defmodule SomeModule do
    @my_custom_config_section (Application.get_env(:my_app, :common)[:key1][:key2])

    IO.puts("@my_custom_config_section debug1: #{Kernel.inspect(@my_custom_config_section)}")
    IO.puts("@my_custom_config_section debug2: #{@my_custom_config_section}")


    # ..........

在尝试运行它时-在编译时遇到了这个问题

    @my_custom_config_section (Application.get_env(:my_app, :common)[:key1][:key2])

    # goood! the values are correct!

    @my_custom_config_section debug1: %{aaa: "", bbb: "fdsafds", ccc: false, dddd: "afdsafdsfdsfds"}


    # also good!
  == Compilation error in file lib/my_app/some_module.ex ==
  ** (Protocol.UndefinedError) protocol String.Chars not implemented for  %{aaa: "", bbb: "fdsafds", ccc: false, dddd: "afdsafdsfdsfds"}

但是!这会打印一个空字符串

  defmodule SomeModule do
    @my_custom_config_section (Application.get_env(:my_app, :common)[:key1][:key2])

    #IO.puts("@my_custom_config_section debug1: #{Kernel.inspect(@my_custom_config_section)}")
    #IO.puts("@my_custom_config_section debug2: #{@my_custom_config_section}")


    # <--------- emtpy!
    # @my_custom_config_section debug3: 
    IO.puts("@my_custom_config_section debug3: #{Kernel.inspect(@my_custom_config_section)}")

这在@my_custom_config_section中什么都找不到:

    def my_action(conn, params) do
      a1 = @my_custom_config_section

      # a1 is empty! Why???


    end

在编译时,数据存在。在运行时-不再。

0 个答案:

没有答案