[erlang-questions] Re: Concept of Side-effect
Fred Hebert (MononcQc)
mononcqc@REDACTED
Fri Sep 18 15:44:16 CEST 2009
On Fri, Sep 18, 2009 at 9:03 AM, Ulf Wiger
<ulf.wiger@REDACTED>wrote:
> Jayson Vantuyl wrote:
>
>> Actually, I'm not entirely sure that an exception would be a side effect.
>> It doesn't get "returned" like a normal value, but I don't see that it
>> changes state any more than a return value does.
>>
>
> It straddles the fence in some ways. Functions can be pure for all
> legal input and raise exceptions for illegal input. This is fine,
> but there are also functions that raise exceptions even for legal
> input.
>
> Consider the following code:
>
> pmap(F, L) ->
> Pids = [spawn_monitor(fun() ->
> exit({ok, F(X)})
> end) || X <- L],
> [receive {'DOWN',Ref,_,_,R} -> unwrap(R) end
> || {_,Ref} <- Pids].
>
> (A simplified version of slide 7 in this talk:
>
> http://www.erlang-factory.com/upload/presentations/56/UlfWiger_ErlangMulticoreEF.pdf
> )
>
> While it might seem contrived, the shell evaluator
> does roughly the same thing: evaluates the expression
> inside an exit() call in order to deliver the result and
> die in one and the same action.
>
> Such functions are only meaningful when considering their
> side-effects - the exceptions.
>
> BR,
> Ulf W
> --
> Ulf Wiger
> CTO, Erlang Training & Consulting Ltd
> http://www.erlang-consulting.com
>
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org
>
>
Wouldn't the ability to just drop in and either catch errors and exit signal
or trap them in a monitor mean exceptions and errors are not necessarily
side effects, no matter the purpose of the function?
Tthe caller's side doing:
case catch Mod:Fun(Args) of
{ok, X} -> X;
{'EXIT',Reason} -> ...
end.
Pretty much ends up transforming an error into a returnable value. There is
no visible difference between the function called having an error or just
returning {'EXIT',Reason} as a normal value, no matter the intent of the
programmer.
(I guess what I'm saying is a bit similar to what you are saying, in the
end)
More information about the erlang-questions
mailing list