[erlang-questions] Bad warning when using short-circuit booleanexpressions

Anthony Molinaro nym-erlang-q@REDACTED
Fri Mar 13 22:02:27 CET 2009


I understand that element/2 is expecting a tuple, but the andalso
is short circuiting when D is not a tuple, so element is always called
with a tuple in this case.  The warning is therefore misleading, since
it states "this expression will fail" when it does not fail.  This may
just be impossible to detect because of the lack of types, in that case
I would be curious how to achieve the same functionality (testing that
an erlang term is a dict in a guard), without the warning.

Thanks,

-Anthony

On Fri, Mar 13, 2009 at 03:06:45PM -0500, Sibley, Jamaal wrote:
>  Sorry, I didn't see the macro.  Element/2 is expecting a tuple as the
> second argument. 
> 
> -----Original Message-----
> From: erlang-questions-bounces@REDACTED
> [mailto:erlang-questions-bounces@REDACTED] On Behalf Of Anthony
> Molinaro
> Sent: Friday, March 13, 2009 1:37 PM
> To: erlang-questions@REDACTED
> Subject: [erlang-questions] Bad warning when using short-circuit
> booleanexpressions
> 
> Hi,
> 
>   I have this code
> 
> -module (is_dict).
> 
> -include_lib ("eunit/include/eunit.hrl").
> 
> -define (is_dict (D), is_tuple (D) andalso element (1, D) =:= dict).
> 
> is_dict_test () ->
>   ?assertEqual (false, ?is_dict (a)),
>   ?assertEqual (false, ?is_dict ({a, b})),
>   ?assertEqual (false, ?is_dict ([{a, b}])),
>   ?assertEqual (true, ?is_dict (dict:new())),
>   ?assertEqual (true, ?is_dict (dict:from_list ([{a, b}]))).
> 
> which gives
> 
> Erlang (BEAM) emulator version 5.6.5 [source] [smp:2] [async-threads:0]
> [kernel-poll:false]
> 
> Eshell V5.6.5  (abort with ^G)
> 1> c (is_dict).
> ./is_dict.erl:8: Warning: this expression will fail with a 'badarg'
> exception
> ./is_dict.erl:10: Warning: this expression will fail with a 'badarg'
> exception {ok,is_dict}
> 2> is_dict:test ().
>   Test successful.
> ok
> 3> 
> 
> The warnings seem wrong as the expression does not fail (or maybe I'm
> just doing something wrong with this test?).
> 
> -Anthony
> 
> --
> ------------------------------------------------------------------------
> Anthony Molinaro                                           <nym@REDACTED>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
> 
> ------------------------------------------------------------------------------
> This communication, along with any documents, files or attachments, is intended only for the use of the addressee and may contain legally privileged and confidential information. If you are not the intended recipient, you are hereby notified that any dissemination, distribution or copying of any information contained in or attached to this communication is strictly prohibited. If you have received this message in error, please notify the sender immediately and destroy the original communication and its attachments without reading, printing or saving in any manner. Thank you.
>  
> CIRCULAR 230 DISCLOSURE: Pursuant to regulations governing practice before the Internal Revenue Service, unless otherwise expressly indicated, any tax advice contained in this communication, including any attachments, is not intended or written by us to be used, and cannot be used, by anyone (i) for the purpose of avoiding tax penalties that may be imposed under the Internal Revenue Code or (ii) for promoting, marketing or recommending to another party any tax-related matters or programs addressed herein.
> 
> 

-- 
------------------------------------------------------------------------
Anthony Molinaro                                           <nym@REDACTED>



More information about the erlang-questions mailing list