除了你可以在其他语言中找到的数据原语和结构之外,Erlang中的完整类型列表是什么?
例如,套接字句柄的类型是什么?那ets处理怎么样?
而且,哪些类型不可能在节点之间进行序列化和交换?我认为套接字句柄必须是其中之一,对吧?
即使在同一节点内的进程中,套接字句柄也是共享的,对吧?那是无共享原则的例外吗?
GC对这种共享内容的行为是什么?什么是Erlang中的套接字实现?我认为这不是一个港口,对吧?
erlang中的类型非常少,您可以参考erlang模块的_ ???(Term)函数来获取内置基本类型的列表:
还有第二个从基本类型派生的列表:
然后,您可以考虑根据这些基本类型的任意组合创建无限数量的类型.在某些程序中,您将看到一些声明,例如:
-type orddict() :: [{Key :: term(), Value :: term()}].
Run Code Online (Sandbox Code Playgroud)
要么
-spec is_key(Key, Orddict) -> boolean() when
Key :: term(),
Orddict :: orddict().
Run Code Online (Sandbox Code Playgroud)
这些信息不是由Erlang编译器直接使用的,它们由拨号器等外部工具使用,并不是获取有效代码所必需的.Erlang的主要特性不是类型声明,而是模式匹配.因此,如果您调用函数并期望表单的返回值,{ok,Value}或者{error,Reason} 您将编写如下内容:
Result = case f(Par) of
{ok,Value} -> resultWhenOk(Value);
{error,Reason} -> resultWhenError(Reason)
end;
Run Code Online (Sandbox Code Playgroud)
或者如果您不关心错误管理:
% get the Name and Age of the employee whose id is Id in a list of people List
% using a function that return a tuple of the form
% {PeopleType, Id, Name,Surname,Age,Sex}
{ok,{employee,Id,Name,_,Age,_}} = find_people(Id,List);
Run Code Online (Sandbox Code Playgroud)