【发布时间】:2018-09-14 03:29:34
【问题描述】:
我是动态创建 ets 表,所以最好避免使用 atom 作为名称。
简单使用字符串作为名称,如::ets.new("aaa", [:named_table])
但是无法编译:
** (ArgumentError) argument error
(stdlib) :ets.new("aaa", [])
【问题讨论】:
我是动态创建 ets 表,所以最好避免使用 atom 作为名称。
简单使用字符串作为名称,如::ets.new("aaa", [:named_table])
但是无法编译:
** (ArgumentError) argument error
(stdlib) :ets.new("aaa", [])
【问题讨论】:
如果您要动态创建 ETS 表,一种方法是将它们创建为未命名的表,并使用 :ets.new 返回的表 ID 来访问它们:
iex(1)> table1 = :ets.new(:foo, [])
8212
iex(2)> table2 = :ets.new(:foo, [])
12309
iex(3)> :ets.insert(table1, {:a, 1})
true
iex(4)> :ets.insert(table2, {:a, 2})
true
iex(5)> :ets.lookup(table1, :a)
[a: 1]
iex(6)> :ets.lookup(table2, :a)
[a: 2]
(在 Erlang/OTP 20.0 中,表 id 是引用而不是整数,但它的工作方式相同;请参阅 this question。)
【讨论】:
{string_name, reference}的地图?
erlang:ref_to_list 和 erlang:list_to_ref,但是文档说“这个 BIF 用于调试,不能在应用程序中使用”,所以我会使用来自的显式映射对引用的响应 ID。