[英]Who can explain the strange result when I use Module names in nested modules in Elixir?
[英]Elixir: Module names with dot and nested modules - are they equivalent?
以下代碼是否相同? 至於調用模塊方法,在兩種情況下,都將使用Utilities.StringUtils.some_method(...)
嵌套模塊
defmodule Utilities do
defmodule StringUtils do
end
end
名稱中帶點的模塊
defmodule Utilities.StringUtils do
end
是的,不是。 第一個定義根據模塊名稱自動定義別名:
defmodule Utilities do
defmodule StringUtils do
end
# Can access the module as StringUtils
end
而第二個:
defmodule Utilities.StringUtils do
# Cannot access the module as StringUtils
end
除此之外,兩者定義的代碼和模塊完全相同。
是的,兩者都被精確地翻譯成符號(在Erlang中,模塊由其符號引用):
:"Elixir.Utilities.StringUtils"
Erlang中沒有真正嵌套的模塊,它只是Elixir模擬的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.