gpt4 book ai didi

erlang - 为什么 Elixir/Erlang 中的数据共享不适用于两个映射中的同一个元组

转载 作者:行者123 更新时间:2023-12-01 01:43:30 26 4
gpt4 key购买 nike

鉴于以下代码

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.am3.b不一样的内存中元组?

最佳答案

现代更新:似乎它已在 ≈v1.7 中修复。

这仅适用于 Elixir;在 Erlang tuple is shared :

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 duplicates maps或喜欢。我认为这可能是对 Elixir 核心的一个很好的错误报告。

在您的示例中 :erts_debug.same(m1.a, m2.a)版画 true仅因 :erts_debug.same(m1, m2) #⇒ true ,例如 map 本身共享相同的内存。

关于erlang - 为什么 Elixir/Erlang 中的数据共享不适用于两个映射中的同一个元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53868207/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com