[erlang-questions] prettypr and spec

36 views
Skip to first unread message

Roberto Ostinelli

unread,
Sep 25, 2012, 12:00:00 AM9/25/12
to Erlang
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.

Anthony Ramine

unread,
Sep 25, 2012, 4:26:21 AM9/25/12
to Roberto Ostinelli, Erlang
Though it doesn't support erl_syntax trees, erl_pp does support pretty-printing of specs and types.

Regards,

--
Anthony Ramine

Le 25 sept. 2012 à 06:00, Roberto Ostinelli a écrit :

> What happens to spec? What is the best way to use an erlang auto-formatter?
>
> Any input welcome.

_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions

Ulf Wiger

unread,
Sep 25, 2012, 4:34:15 AM9/25/12
to Roberto Ostinelli, Erlang

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.


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

_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions

Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.



Roberto Ostinelli

unread,
Sep 25, 2012, 12:25:15 PM9/25/12
to Ulf Wiger, Erlang
Thank you Anthony, Ulf,

I'll definitely look into that. I just felt curious when I saw


-spec({{greetings, 1},
       [{type, 12, 'fun',
 [{type, 12, product,
   [{ann_type, 12,
     [{var, 12, 'Text'}, {type, 12, string, []}]}]},
  {type, 12, string, []}]}]}).

hem... ^^_

r.

Roberto Ostinelli

unread,
Sep 25, 2012, 4:31:00 PM9/25/12
to Ulf Wiger, Erlang
Thank you Ulf,

I've looked at this and it reads from .beam files. Same problem as before: as specified I need to parse string() code.

Do I really have to write my own erlang indenter? That really seems weird. Isn't there such a thing like a DSL for indenters?



On Tue, Sep 25, 2012 at 1:34 AM, Ulf Wiger <u...@feuerlabs.com> wrote:

Ulf Wiger

unread,
Sep 25, 2012, 6:48:36 PM9/25/12
to Roberto Ostinelli, Erlang
Someone really should fix the bugs in erl_prettypr.

BR,
Ulf W

Ulf Wiger, Feuerlabs, Inc.

Steve Davis

unread,
Sep 27, 2012, 9:48:35 PM9/27/12
to erlang-pr...@googlegroups.com, Erlang, rob...@widetag.com
I'd say someone decided to waste your brain-time. i.e. in making an apparent requirement of the -spec part of the code (as per the other thread I started).
 
Any input welcome.

r.

Reply all
Reply to author
Forward
0 new messages