November 14th Saturday

Erlang中每个进程都有一个私有的数据存储区——进程字典,它由一组键值对组成。本文介绍了如何使用特定的内置函数来操作这个字典,包括添加、查找、删除键值对等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The Process Dictionary

 

  Each process in Erlang has its own private data store called the process dictionary.  The process dictionary is an assciative array (in other languages this might be called a map, hashmap, or hash table) coposed of a collection of keys and values.  Each key has only one value.

 

  The dictionary can be manipulated using the following BIFs:

 

@spec put(Key, Value) -> OldValue.

    Add a Key, Value association to the process dictionary.  The value of put is OldValue, which is the  previous value associated with Key.  If there was no previous value, the atom undefined is returned.

 

@spec get(Key) -> Value.

    Look up the value of Key.  If there is an association Key, Value association in the dictionary, return Value;  Otherwise, return the atom undefined.

 

@spec get() -> [{Key, Value}].

    Return the entire dictionary as a list of {Key, Value} tuples.

 

@spec get_keys(Value) -> [Key].

    Return a list of keys that have the values Value in the dictionary.

 

@spec erase(Key) -> Value.

    Return the value associated with Key or the atom undefined if there is no value associated with Key.  Finally, erase the value associated with Key.

 

@spec erase() -> [{Key, Value}].

    Erase the entire process dictionary.  The return value is a list of {Key, Value} tuples representing the state of the dictionary before it was erased.

 

  If you use the process dictionary, your code will no longer be side effect free, and all the  benefits of using nondestructive variables.

 

  Using the process dictionary can introduce subtle bugs into your program and make it difficult to debug.  One form of usage that I do approve of is to use the processes dictionary to store “write-once” variables.  If a key acquires a value exactly once and does not change the value, then storing it in the process dictionary is sometimes acceptable.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值