3 ms·
And in Erlang: 1> X = #{"a" => 1}. #{"a" => 1} 2> maps:find("a", X). {ok,1} 3> maps:find("x", X). error Constructing and destructuring
by derefr 12y ago
And in Erlang:
1> X = #{"a" => 1}.
#{"a" => 1}
2> maps:find("a", X).
{ok,1}
3> maps:find("x", X).
error
Constructing and destructuring tuples is cheap in the Erlang VM, so Erlang uses them where other languages would use types.
Further, in idiomatic Erlang, you don’t bother with a branch for each case of the option type; you just do the following:
{ok, Val} = maps:find("a", X).
Effectively, that's a type assertion: if maps:find/1 responds with error instead of {ok, _}, that’s a process crash.