Tip of the day

Eric Newhuis <>
Fri Aug 13 17:01:22 CEST 2004


Any way to get module and line-number information when there is a badmatch?

Our unit tests would be happier and traced failures could enable activation
of the appropriate source file.

We use macros like this...

%% ?MATCH
%%
%% Generates badmatch error with module and line info if X does not match Y.
%%
%% usage:  ?MATCH (A,A)

-define (MATCH (X, Y), {?MODULE, ?LINE, X} = {?MODULE, ?LINE, Y}).



On 8/13/04 9:41 AM, "Joe Armstrong" <> wrote:

> Brilliant
> 
> 
>     How come we miss the simplest of ideas?
> 
>     I have hundreds (thousands?) of lines of
> 
>     {ok, XX} =foo(...)
> 
>    ok(foo(..)) is  *much clearer* -  and it saves two  characters (well
> one really, but I always put a space after the comma.
> 
>     I'm going to have to change a lot of my code now :-)
> 
>     /Joe
> 
> 
> 
> 
> 
> On Thu, 12 Aug 2004, Richard Carlsson wrote:
> 
>> 
>> Put the following in your user_default.erl
>> 
>> ----cut here----
>> -module(user_default).
>> -compile(export_all).
>> 
>> 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
>> 
>>    code:add_path("...path-to-my-user-default/ebin").
>>    code:ensure_loaded(user_default).
>> 
>> 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])).
>>  <0.40.0>
>>  2> file:close(File).
>> 
>> or
>> 
>>  3> ok(erl_parse:parse_term(ok(erl_scan:string("[1,2,3].")))).
>>  [1,2,3]
>>  4>
>> 
>> No more matching on {ok, ...}. (Works for tuples of any size > 1.)
>> 
>> /Richard
>> 
>> 
>> 
>> 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
>> 

Eric Newhuis
Chief Technology Officer
FutureSource
630.649.9619 Mobile
630.792.2065 Office
630.792.2600 Fax

http://www.ifuturesource.com

FutureSource | Futures Solutions. Now.

The information contained in this e-mail message is legally privileged and
confidential information intended only for the use of the individual or
entity named above. If the reader of this e-mail message is not the intended
recipient, you are hereby notified that any dissemination, distribution or
copy of this e-mail is strictly prohibited. If you have received this e mail
in error, please contact the sender and immediately delete the original
message. Thank you.





More information about the erlang-questions mailing list