Hidden Features of Erlang
10,431
Solution 1
The magic commands in the shell. The full list is in the manual, but the ones I use most are:
- f() - forget all variables
- f(X) - forget X
- v(42) - recall result from line 42
- v(-1) - recall result from previous line
- e(-1) - reexecute expression on previous line
- rr(foo) - read record definitions from module foo
- rr("*/*") - read record definitions from every module in every subdirectory
- rp(expression) - print full expression with record formating
Solution 2
Inheritance! http://www.erlang.se/euc/07/papers/1700Carlsson.pdf
Parent
-module(parent).
-export([foo/0, bar/0]).
foo() ->
io:format("parent:foo/0 ~n", []).
bar() ->
io:format("parent:bar/0 ~n", []).
Child
-module(child).
-extends(parent).
-export([foo/0]).
foo() ->
io:format("child:foo/0 ~n", []).
Console
23> parent:foo().
parent:foo/0
ok
24> parent:bar().
parent:bar/0
ok
25> child:foo().
child:foo/0
ok
26> child:bar().
parent:bar/0
ok
Solution 3
Parameterized Modules! From http://www.lshift.net/blog/2008/05/18/late-binding-with-erlang and http://www.erlang.se/euc/07/papers/1700Carlsson.pdf
-module(myclass, [Instvar1, Instvar2]).
-export([getInstvar1/0, getInstvar2/0]).
getInstvar1() -> Instvar1.
getInstvar2() -> Instvar2.
And
Eshell V5.6 (abort with ^G)
1> Handle = myclass:new(123, 234).
{myclass,123,234}
2> Handle:getInstvar1().
123
3> Handle:getInstvar2().
234
Solution 4
user_default.erl - you can build your own shell builtins by having a compiled user_default.beam in your path which can be pretty nifty
Solution 5
beam_lib:chunks can get source code from a beam that was compiled with debug on which can be really usefull
{ok,{_,[{abstract_code,{_,AC}}]}} = beam_lib:chunks(Beam,[abstract_code]).
io:fwrite("~s~n", [erl_prettypr:format(erl_syntax:form_list(AC))]).
Related videos on Youtube
Comments
-
pageman about 2 years
In the spirit of:
- Hidden Features of C#
- Hidden Features of Java
- Hidden Features of ASP.NET
- Hidden Features of Python
- Hidden Features of HTML
- and other Hidden Features questions
What are the hidden features of Erlang that every Erlang developer should be aware of?
One hidden feature per answer, please.
-
Ólafur Waage almost 15 yearsPlease Community Wiki this.
-
ryeguy almost 15 yearsHow is this a hidden feature? A notable feature for sure, but this is one of the language's primary boasting points, not hidden at all.
-
John almost 15 yearsI spotted this when I was trying to grok the mochiweb source. Took a while to google what the hell it was actually doing, as the syntax was totally different to the Erlang I've seen before.
-
Christian almost 15 yearsAnd using rp(expression(...)) to have the result printed out without pretty-printing too deeply nested structures, instead it prints it fully
-
Paul Ankman about 14 yearsDidn't know that, very useful, thanks!
-
bjnortier about 14 yearsYup. Interesting! Although I haven't seen it in any libraries I've used to date...
-
YasirA almost 14 yearsHowever it's not welcomed to use parameterized modules: They add variables to functions and it's not good when it comes to debug them. They break the functional paradigm adding a layer of complexity introduced by variables you should keep track of (like global variables in imperative languages), they are immutable but destroy transparency. There've been the thoughts that they (parameterized modules) still exist in the language only because of products utilizing them, mochiweb is an example. Just think, why appeared in 2003 they still officially undocumented?
-
BalusC almost 14 yearsFor the case you wonders the downvotes, this was an accident, please check this meta topic. I'll edit your question so that they can be removed.
-
bjnortier over 13 yearsThis comment belongs to the "Parameterized Modules" answer...
-
Anton Kraievyi over 12 yearsBut why the hell no upvotes? :D