简体   繁体   中英

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", [])

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 .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM