[erlang-questions] prettypr and spec

Ulf Wiger ulf@REDACTED
Tue Sep 25 10:34:15 CEST 2012


Roberto,

It appears as if erl_pp:form/1 does a much better job of formatting specs,
but then, of course, you lose the comments.

There is also an issue with typed record fields, which seems to affect 
erl_prettypr as well. It simply removes the types from record declarations.

Take a look at parse_trans_pp:beam/1. It makes no effort to preserve comments
(naturally, since it doesn't expect to find any), but handles specs and typed 
records correctly.

https://github.com/uwiger/parse_trans/blob/master/src/parse_trans_pp.erl#L71

This module, BTW, is also made to be used as an escript, and I have the 
following convenient alias:

alias pp='escript /Users/uwiger/git/parse_trans/ebin/parse_trans_pp.beam'

Not that the last point was particularly relevant to your particular problem. :)

BR,
Ulf W

On 25 Sep 2012, at 06:00, Roberto Ostinelli wrote:

> I have this code:
> 
> %%%%%%%%%%%%%%%%%%%%%%%%
> -module(mymodule).
> -export([hello/0, goodbye/0, greetings/1]).
> 
> -spec hello() -> string().
> hello() ->
> 	greetings("hello").
> 
> -spec goodbye() -> string().
> goodbye() ->
> 	greetings("goodbye").
> 
> -spec greetings(Text::string()) -> string().
> greetings(Text) ->
> 	lists:flatten(io_lib:format("~s sublimerl", [Text])).
> 
> -ifdef(TEST).
> -include_lib("eunit/include/eunit.hrl").
> 
> simple_test() ->
> 	?assertEqual("hey sublimerl", greetings("hey")).
> 
> greetings_test_() ->
> 	[?_assertEqual("hey sublimerl", greetings("hey"))].
> 
> -endif.
> %%%%%%%%%%%%%%%%%%%%%%%%
> 
> 
> when i convert it using this:
> 
> 
> %%%%%%%%%%%%%%%%%%%%%%%%
> Formatted = case epp_dodger:parse_file(FilePath) of
> 		{ok, Forms} ->
> 			case lists:keymember(error, 1, Forms) of
> 				false ->
> 					erl_prettypr:format(erl_recomment:recomment_forms(Forms, erl_comment_scan:file(FilePath)));
> 				true ->
> 					""
> 			end;
> 		{error, _} ->
> 			""
> 	end
> %%%%%%%%%%%%%%%%%%%%%%%%
> 
> 
> I get:
> 
> %%%%%%%%%%%%%%%%%%%%%%%%
> -module(mymodule).
> 
> -export([hello/0, goodbye/0, greetings/1]).
> 
> -spec({{hello, 0},
>        [{type, 4, 'fun',
> 	 [{type, 4, product, []}, {type, 4, string, []}]}]}).
> 
> hello() -> greetings("hello").
> 
> -spec({{goodbye, 0},
>        [{type, 8, 'fun',
> 	 [{type, 8, product, []}, {type, 8, string, []}]}]}).
> 
> goodbye() -> greetings("goodbye").
> 
> -spec({{greetings, 1},
>        [{type, 12, 'fun',
> 	 [{type, 12, product,
> 	   [{ann_type, 12,
> 	     [{var, 12, 'Text'}, {type, 12, string, []}]}]},
> 	  {type, 12, string, []}]}]}).
> 
> greetings(Text) ->
>     lists:flatten(io_lib:format("~s sublimerl", [Text])).
> 
> -ifdef(TEST).
> 
> -include_lib("eunit/include/eunit.hrl").
> 
> simple_test() ->
>     ?assertEqual("hey sublimerl", (greetings("hey"))).
> 
> greetings_test_() ->
>     [?_assertEqual("hey sublimerl", (greetings("hey")))].
> 
> -endif.
> %%%%%%%%%%%%%%%%%%%%%%%%
> 
> 
> What happens to spec? What is the best way to use an erlang auto-formatter?
> 
> Any input welcome.
> 
> r.
> 
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions

Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.
http://feuerlabs.com



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120925/27c58b5d/attachment.htm>


More information about the erlang-questions mailing list