LoranceChen
LoranceChen

Reputation: 2574

why Erlang string can't used as ets table name?

I'm dynamic create ets table, so it's better avoid atom as name.
Simple use string as name, such as:
:ets.new("aaa", [:named_table])

But it can't be compiled:

** (ArgumentError) argument error
    (stdlib) :ets.new("aaa", [])

Upvotes: 2

Views: 1187

Answers (1)

legoscia
legoscia

Reputation: 41528

If you're creating ETS tables dynamically, one way to do it is to create them as unnamed tables, and use the table id returned by :ets.new to access them:

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]

(In Erlang/OTP 20.0, the table id is a reference instead of an integer, but it works the same way; see this question.)

Upvotes: 6

Related Questions