[erlang-questions] chained functions

Robert Virding <>
Wed Jan 25 10:06:03 CET 2012


I personally am ok with "staircasing" so I don't see the problem. :-) This probably comes from working with it for a long time. Also I find that very often the handling of returns values is more complex than just chaining things through. I also feel there is a benefit in being very explicit with values being returned so you see what is happening.

You tag the return value of a function when you need to clearly show whether the function has "succeeded" or "failed". Often the value in itself is not enough. For example a value could both be a valid return and an error reason which case you need to tag it to differentiate between them.

Robert

----- Original Message -----
> 
> A friend of mine asked:
> 
> ##
> If you have functions that return {ok, Result} | {error, Reason}
> how do you chained them? So that you have:
> 
> w(x(y(z(...))))
> 
> without building a staircasing. Something that would be done in
> Haskell
> with monads.
> ##
> 
> I would probably go for:
> 
> x({ok, Value}) ->
>   NewValue = <do something with Value>,
>   {ok, NewValue};
> x({error, Reason}) ->
>   {error, Reason}.
> 
> in each function
> 
> which brings me other question, when do you tag return values?
> 
> I tend to only use tagged return values with impure functions, were
> an
> error is more likely due to side effects.
> 
> --
> Reynaldo
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
> 



More information about the erlang-questions mailing list