[英]Why is data sharing in Elixir / Erlang not working for the same tuple in two maps
給出以下代碼
defmodule Test do
def run do
p1 = {1, 2}
m1 = %{a: p1}
m2 = %{a: p1}
IO.puts :erts_debug.same(m1.a, m2.a)
m3 = %{b: p1}
IO.puts :erts_debug.same(m1.a, m3.b)
end
end
為什么Test.run
打印出來
iex(1)> Test.run
true <--- expected
false <--- not true ?!
:ok
為什么m1.a
和m3.b
在內存元組中不一樣?
現代時代的更新:似乎它已在≈v1.7中修復。
僅適用於Elixir; 在Erlang 中共享元組 :
1> Tuple = {1, 2},
1> Key1 = 1,
1> Key2 = 2,
1> Map1 = #{1 => Tuple, 2 => Tuple},
1> erts_debug:same(maps:get(Key1,Map1), maps:get(Key2,Map1)).
true
2> Key3 = 3,
2> Map2 = #{3 => Tuple},
2> erts_debug:same(maps:get(Key1,Map1), maps:get(Key3,Map2)).
true
對於Elixir,這可能是因為內部轉換器到erlang 重復地圖等。 我想這可能是Elixir核心的一個很棒的錯誤報告。
在您的示例中:erts_debug.same(m1.a, m2.a)
僅在:erts_debug.same(m1, m2) #⇒ true
打印為true
:erts_debug.same(m1, m2) #⇒ true
,例如地圖本身共享相同的內存。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.