Tip of the day

Richard Carlsson <>
Thu Aug 12 19:05:25 CEST 2004

Put the following in your user_default.erl

----cut here----

ok(T) when tuple(T), size(T) >= 2, element(1, T) == ok -> element(2, T);
ok(E) -> erlang:fault({not_ok, E}).

ok2(T) when tuple(T), size(T) >= 3, element(1, T) == ok -> element(3, T);
ok2(E) -> erlang:fault({not_ok, E}).

ok3(T) when tuple(T), size(T) >= 4, element(1, T) == ok -> element(4, T);
ok3(E) -> erlang:fault({not_ok, E}).
----cut here----

and add the lines


to your ~/.erlang file.

Then you can use these functions in the Erlang shell, like this:

  Eshell V5.4  (abort with ^G)
  1> File = ok(file:open("myfile",[read])).
  2> file:close(File).


  3> ok(erl_parse:parse_term(ok(erl_scan:string("[1,2,3].")))).

No more matching on {ok, ...}. (Works for tuples of any size > 1.)


Richard Carlsson ()   (This space intentionally left blank.)
E-mail: 	WWW: http://user.it.uu.se/~richardc/
 "Having users is like optimization: the wise course is to delay it."
   -- Paul Graham

More information about the erlang-questions mailing list