[erlang-questions] guard expression restriction

Hynek Vychodil hynek@REDACTED
Fri Dec 3 11:20:44 CET 2010


On Fri, Dec 3, 2010 at 3:44 AM, Jeff Schultz <jws@REDACTED> wrote:
> On Thu, Dec 02, 2010 at 10:37:55AM +0200, Kostis Sagonas wrote:
>> Hmmm... In any case, the paper I mentioned in my previous mail contains an
>> example of more clear and succinct code when user-defined guards are
>> allowed.
>
> I'm pretty sure I wouldn't write your Figure 7 that way in anything
> other than short-lived code that I planned to throw away.*
>
> Transcribed, it's more or less
>
>    foo2(Set) when gb_sets:is_set(Set) ->
>        handlegbset(Set);
>    foo2(Set) when sets:is_set(Set) ->
>        handleset(Set);
>    foo2(_) ->
>        error.
>
> This puts my code at the mercy of data-representation changes in
> either referenced module that I have no control over.  (In fact,
> gb_sets:is_set is explicitly "Not Recommended" in its documentation.)
>
> I'd be much more likely to write something like
>
>    -record(agg, {kind=error, set=error}).
>
>    % Code to generate various kinds of #agg and pack them correctly
>    % into the record . . . .
>
>    foo3(Agg) when Agg#agg.kind =:= gb_set ->
>        handlegbset(Agg);
>    foo3(Agg) when Agg#agg.kind =:= set ->
>        handleset(Agg);
>    foo3(_) ->
>        error.
>

Even better but point taken

foo3(#agg{kind = gb_set } = Agg) ->
     handlegbset(Agg);
foo3(#agg{kind = set      } = Agg) ->
      handleset(Agg);
foo3(_) ->
      error.


> I can maintain this, and I'm safe from changes in someone else's
> datastructures that might make one kind of collection look like
> another.
>
>
>    Jeff Schultz
>
>
> -----------------------------------------------------
> *Of course, it's often the code you were most certain
> to throw away that lives the longest :-(
>
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
>
>



-- 
--Hynek (Pichi) Vychodil

Analyze your data in minutes. Share your insights instantly. Thrill
your boss.  Be a data hero!
Try GoodData now for free: www.gooddata.com


More information about the erlang-questions mailing list