[erlang-questions] Style question: accessing record fields in a function clause pattern match?

Dmitry Belyaev be.dmitry@REDACTED
Wed Jun 18 01:22:28 CEST 2014


Why not to use different records for different contents? 
-- 
Best wishes,
Dmitry Belyaev

On 18 June 2014 12:21:15 AM AEST, Roger Lipscombe <roger@REDACTED> wrote:
>I'm currently refactoring some code, and I've got something like this:
>
>handle_foo(#state { type = bar, id = Id }) -> whatever;
>handle_foo(#state { type = baz, parent_id = ParentId }) -> whatever.
>
>...and I'm wondering whether it's considered bad form to access record
>fields at the same time as matching them in a function clause. I'm
>matching on 'type', and accessing 'id' at the same time, I mean.
>
>Instead, should I consider something like the following?
>
>handle_foo(#state { type = bar }) ->
>    Id = State#state.id,
>    whatever;
>handle_foo(#state { type = baz }) ->
>    ParentId = State#state.parent_id,
>    whatever.
>
>The first form makes it explicit that for different types, different
>fields matter (which might be a smell by itself), but it gets hard to
>see the match on 'type' once I'm accessing more than one or two
>fields.
>
>I know this is stylistic, and there's no "right" answer; I just
>wondered what other people tend to do?
>
>Or, more generally, do people prefer using pattern matching to access
>record values, or plain-ol' Foo = State#state.foo expressions?
>
>Thanks,
>Roger.
>_______________________________________________
>erlang-questions mailing list
>erlang-questions@REDACTED
>http://erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20140618/6766df1e/attachment.htm>


More information about the erlang-questions mailing list