Matching elements of records

Hakan Mattsson hakan@REDACTED
Wed Oct 27 15:26:20 CEST 2004


On 27 Oct 2004, Bjorn Gustavsson wrote:

BG> Date: 27 Oct 2004 12:40:45 +0200
BG> From: Bjorn Gustavsson <bjorn@REDACTED>
BG> To: erlang-questions@REDACTED
BG> Subject: Re: Matching elements of records
BG> 
BG> The compiler will generate slightly more efficient code for
BG> test1/1.
BG> 
BG> Also, I find test1/1 much easier to read. It is the style I
BG> use all the time (except that I place the Foo variable to
BG> right of the #foo{} pattern, but that is just a matter of
BG> personal preference).

I prefer a third style:

  test3(#foo{one = One} = Foo) ->
      case One of
          1 ->
             is_one;
          2 ->
             is_two
      end.

/Håkan

BG> 
BG> /Bjorn
BG> 
BG> Fredrik Thulin <ft@REDACTED> writes:
BG> 
BG> > Hi
BG> > 
BG> > In my application, I find that I often pass records to functions and 
BG> > want to end up in different instances of the functions depending on 
BG> > some element of the record.
BG> > 
BG> > I have done this like test2() below does in a number of places, but I 
BG> > would want to to reduce length of fun() lines and to avoid problems in 
BG> > the emacs-mode indentation (*) with '#' after 'when'.
BG> > 
BG> > Considering the following example code (as far as I can tell, both test1
BG> > () and test2() accomplishes what I want), is any of them crazy or in 
BG> > some way worse than the other?
BG> > 
BG> > 
BG> > -module(t).
BG> > -compile(export_all).
BG> > 
BG> > -record(foo, {one, two}).
BG> > 
BG> > 
BG> > test1(Foo=#foo{one=1}) ->
BG> >     io:format("in test1 #1: ~p~n", [Foo]),
BG> >     is_one;
BG> > test1(Foo=#foo{one=2}) ->
BG> >     io:format("in test1 #2: ~p~n", [Foo]),
BG> >     is_two.
BG> > 
BG> > test2(Foo) when is_record(Foo, foo), Foo#foo.one == 1 ->
BG> >     io:format("in test2 #1: ~p~n", [Foo]),
BG> >     is_one;
BG> > test2(Foo) when is_record(Foo, foo), Foo#foo.one == 2 ->
BG> >     io:format("in test2 #2: ~p~n", [Foo]),
BG> >     is_two.
BG> > 
BG> > do() ->
BG> >     R=#foo{one=2, two=bar},
BG> >     io:format("test1 result : ~p~n", [test1(R)]),
BG> >     io:format("test2 result : ~p~n", [test2(R)]),
BG> >     ok.
BG> > 
BG> > 
BG> > Result :
BG> > 
BG> > 14> t:do().
BG> > in test1 #2: {foo,2,bar}
BG> > test1 result : is_two
BG> > in test2 #2: {foo,2,bar}
BG> > test2 result : is_two
BG> > ok
BG> > 15>
BG> > 
BG> > /Fredrik
BG> > 
BG> > *) Emacs mode (the one distributed with R10B-0) does not align the 
BG> > "Foo#foo.one == 1" with the "is_record(Foo, foo)" if you want to break 
BG> > the line :
BG> > 
BG> > test2(Foo) when is_record(Foo, foo), 
BG> > Foo#foo.one == 1 ->
BG> > 
BG> > rather than 
BG> > 
BG> > test2(Foo) when is_record(Foo, foo), 
BG> >                 Foo#foo.one == 1 ->
BG> > 
BG> > It works with non-record variables, like :
BG> > 
BG> > test2(Foo, Bar) when is_record(Foo, foo), 
BG> >                      Bar == 1 ->





More information about the erlang-questions mailing list