[erlang-questions] Getting rid of some repetition.

Colin Z theczintheroc2007@REDACTED
Tue Mar 18 18:20:54 CET 2008


Cool, thanks for the examples.

The problem still remains of what happens when the function signature
changes.
If loop/3 becomes loop/4, then we still have to change every "branch" of the
receive block. I guess my gripe is that erlang doesn't feel very friendly
from a refactoring standpoint. Some languages have default parameters for
functions, and others you can avoid the whole situation by not having
massive if/else, case, or, in this case, receive blocks.

I think the record example solves the issue, the only drawback being that
you're forced to use a record.

On Tue, Mar 18, 2008 at 9:55 AM, Kenneth Lundin <kenneth.lundin@REDACTED>
wrote:

> On 3/18/08, Mats Cronqvist <mats.cronqvist@REDACTED> wrote:
> > Colin Z wrote:
> > > Say I have a process loop with a receive block that receives a bunch
> > > of different messages...typical scenario:
> > >
> > > loop(A, B, C)->
> > > receive
> > >  {msg1, A1}->
> > >   loop(A1, B, C)
> > > ;
> > > {msg2, B1}->
> > >   loop(A, B1, C)
> > > ;
> > > {msg3, C1}->
> > >   loop(A, B, C1)
> > > ;
> > > {msg4, A2, C2}->
> > >   loop(A2, B, C2)
> > > end
> >  this idiom would reduce the typing;
> >
> > loop(A0, B0, C0)->
> >  receive
> >   {msg1, A1}-> B1=B0,C1=C0;
> >   {msg2, B1}-> A1=A0,C1=C0;
> >   {msg3, C1}-> A1=A0,B1=B0;
> >   {msg4, A1, C1}-> B1=B0;
> >  end,
> >  loop(A1, B1, C1).
> >
> >  the new syntax you propose is a bit too perly for my taste.
> >
> >  mats
> >
> I think this is even better:
>
> loop(A0, B0, C0)->
>  {A,B,C} =
>    receive
>     {msg1, A1}-> {A1,B0,C0};
>     {msg2, B1}-> {A0,B1,C0};
>     {msg3, C1}-> {A0,B0,C1};
>     {msg4, A1, C1}-> {A1,B0,C1};
>   end,
>  loop(A1, B1, C1).
>
> Of course you could have called loop directly from the case alternatives
> as in the original example, but if each alternative need to do
> something more complicated I think this is clear and easy to
> understand.
>
> Mats example is actually more complicated in my view with all the ugly
> B1=B0, ... stuff.
>
> /Kenneth
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080318/4edf54f0/attachment.htm>


More information about the erlang-questions mailing list