Skip to content

Commit

Permalink
stdlib: Fix map type listing
Browse files Browse the repository at this point in the history
  • Loading branch information
psyeugenic committed Mar 27, 2014
1 parent 5b9ffc7 commit 13d1e79
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/stdlib/src/erl_pp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ ltype({type,_Line,nonempty_list,[T]}) ->
{seq,$[,$],[$,],[ltype(T),leaf("...")]};
ltype({type,Line,nil,[]}) ->
lexpr({nil,Line}, 0, options(none));
ltype({type,Line,map,any}) ->
simple_type({atom,Line,map}, []);
ltype({type,_Line,map,Pairs}) ->
map_type(Pairs);
ltype({type,Line,tuple,any}) ->
simple_type({atom,Line,tuple}, []);
ltype({type,_Line,tuple,Ts}) ->
Expand Down Expand Up @@ -289,6 +293,15 @@ binary_type(I1, I2) ->
E2 = [[leaf("_:_*"),lexpr(I2, P, options(none))] || U],
{seq,'<<','>>',[$,],E1++E2}.

map_type(Fs) ->
{first,[$#],map_pair_types(Fs)}.

map_pair_types(Fs) ->
tuple_type(Fs, fun map_pair_type/1).

map_pair_type({type,_Line,map_field_assoc,Ktype,Vtype}) ->
{seq,[],[]," =>",[ltype(Ktype),ltype(Vtype)]}.

record_type(Name, Fields) ->
{first,[record_name(Name)],field_types(Fields)}.

Expand Down
10 changes: 10 additions & 0 deletions lib/stdlib/test/erl_pp_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,16 @@ maps_syntax(Config) when is_list(Config) ->
ok = pp_expr(<<"#{ a => 1, <<\"hi\">> => \"world\", 33 => 1.0 }">>),
ok = pp_expr(<<"#{ a := V1, <<\"hi\">> := V2 } = M">>),
ok = pp_expr(<<"M#{ a => V1, <<\"hi\">> := V2 }">>),
F = <<"-module(maps_type_syntax).\n"
"-compile(export_all).\n"
"-type t1() :: map().\n"
"-type t2() :: #{ atom() => integer(), atom() => float() }.\n"
"-spec f1(t1()) -> 'true'.\n"
"f1(M) when is_map(M) -> true.\n"
"-spec f2(t2()) -> integer().\n"
"f2(#{a := V1,b := V2}) -> V1 + V2.\n"
"\n">>,
ok = pp_forms(F),
ok.


Expand Down

0 comments on commit 13d1e79

Please sign in to comment.