Skip to content

Instantly share code, notes, and snippets.

@archaelus
Created September 8, 2011 22:00
Show Gist options
  • Save archaelus/1204892 to your computer and use it in GitHub Desktop.
Save archaelus/1204892 to your computer and use it in GitHub Desktop.
Fun experiment
-module(crazy_fun).
-export([science/0]).
science() ->
fun () ->
'nothing_to_see_here'
end.
-module(crazy_fun).
-export([science/0]).
science() ->
fun () ->
'something_to_see_here?'
end.
version() ->
three.
Erlang R14B04 (erts-5.8.5) [source] [64-bit] [smp:4:4] [rq:4] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.8.5 (abort with ^G)
1> c(crazy_fun).
{ok,crazy_fun}
2> crazy_fun:
module_info/0 module_info/1 science/0
2> F = crazy_fun:science().
#Fun<crazy_fun.0.71331273>
3> file:write_file("dynamite.erlfun", erlang:term_to_binary(crazy_fun:science())).
ok
4> code:delete(crazy_fun).
true
5> code:purge(crazy_fun).
*** ERROR: Shell process terminated! ***
Eshell V5.8.5 (abort with ^G)
1>
User switch command
--> q
%% crazy_fun module no-longer in code path:
nem@dhcp-164 ~ % erl
Erlang R14B04 (erts-5.8.5) [source] [64-bit] [smp:4:4] [rq:4] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.8.5 (abort with ^G)
1> F = binary_to_term(element(2, file:read_file("tmp/dynamite.erlfun"))).
#Fun<crazy_fun.0.71331273>
2> f().
ok
3> F = binary_to_term(element(2, file:read_file("tmp/dynamite.erlfun"))).
#Fun<crazy_fun.0.71331273>
4> F().
** exception error: {undef,[{#Fun<crazy_fun.0.71331273>,[]},
{erl_eval,do_apply,5},
{shell,exprs,7},
{shell,eval_exprs,7},
{shell,eval_loop,3}]}
5>
User switch command
--> q
%% new version, unchanged fun body
nem@dhcp-164 ~/tmp % erl
Erlang R14B04 (erts-5.8.5) [source] [64-bit] [smp:4:4] [rq:4] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.8.5 (abort with ^G)
1> c(crazy_fun).
./crazy_fun.erl:10: Warning: function version/0 is unused
{ok,crazy_fun}
2> F = binary_to_term(element(2, file:read_file("tmp/dynamite.erlfun"))).
** exception error: bad argument
in function binary_to_term/1
called as binary_to_term(enoent)
3> F = binary_to_term(element(2, file:read_file("dynamite.erlfun"))).
#Fun<crazy_fun.0.71331273>
4> F().
nothing_to_see_here
5>
User switch command
--> q
%% updated fun body
nem@dhcp-164 ~/tmp % erl
Erlang R14B04 (erts-5.8.5) [source] [64-bit] [smp:4:4] [rq:4] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.8.5 (abort with ^G)
1> c(crazy_fun, [load]).
./crazy_fun.erl:10: Warning: function version/0 is unused
{ok,crazy_fun}
2> F = binary_to_term(element(2, file:read_file("dynamite.erlfun"))).
#Fun<crazy_fun.0.71331273>
3> F().
** exception error: bad function #Fun<crazy_fun.0.71331273>
4>
User switch command
--> q
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment