[erlang-questions] Any wisdom to offer on "tagged return values"

Kevin q2h46uw02@REDACTED
Fri May 1 23:17:06 CEST 2009


In the "programming rules" 
http://www.erlang.se/doc/programming_rules.shtml#REF32551


---snip--------------------------------------
Use tagged return values.

Don't program like this:

keysearch(Key, [{Key, Value}|_Tail]) ->
  Value; %% Don't return untagged values!
keysearch(Key, [{_WrongKey, _WrongValue} | Tail]) ->
  keysearch(Key, Tail);
keysearch(Key, []) ->
  false.

Then the {Key, Value} cannot contain the false value. This is the 
correct solution:

keysearch(Key, [{Key, Value}|_Tail]) ->
  {value, Value}; %% Correct. Return a tagged value.
keysearch(Key, [{_WrongKey, _WrongValue} | Tail]) ->
  keysearch(Key, Tail);
keysearch(Key, []) ->
  false.
</unquote>
---/snip--------------------------------------

But I'm skeptical, and the example is really a unique situation.  If 
this was followed slavishly, my code would be littered with {ok, blah},
and it would make "chaining" functions awkward, one(two(three(1))), 
which can really help clean up code.


Ironically in the lists man page it says keysearch is deprecated in 
favor of keyfind, which does exactly what the programming rules
cautions against.

keyfind(Key, N, TupleList) -> Tuple | false













More information about the erlang-questions mailing list