Pid 作为 erlang 映射键?

Pid as erlang maps key?

Pid 可以作为映射键吗?

从#{} 语法构建映射,错误说 Pid 不能是键。

使用地图模块构建错误,Pid 可以是关键。

18> 
18> Pid = self().
<0.39.0>
19> #{Pid => 1}.
* 1: illegal use of variable 'Pid' in map
20> 
20> M1 = maps:from_list([{Pid, 1}]).
#{<0.39.0> => 1}
21> 
21> #{Pid := V} = M1.
* 2: illegal use of variable 'Pid' in map
22>                  
22> maps:get(Pid, M1).
1

问题不在于 pid 的使用,而是在于在映射定义中将变量用作键。还没有实现(不知道什么时候可以实现)。

"Erlang 18 (release candidate 2)" 中已经支持映射模式中的任意键。

$ erl
Erlang/OTP 18 [RELEASE CANDIDATE 2] [erts-7.0] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V7.0  (abort with ^G)
1> Pid = self().
<0.33.0>
2> #{Pid => 1}.
#{<0.33.0> => 1}