[erlang-questions] Type specs depending on build-time selected code
Olivier BOUDEVILLE
olivier.boudeville@REDACTED
Fri Nov 18 15:56:07 CET 2011
Hi,
I suppose there are situations where the actual code for a function is
based on preprocessor defines while we want the type specs to encompass
all possible implementations.
A simple example could be a debug mode which is to be enabled/disabled at
build time:
"""
-module(foo).
-export( [is_debug_mode_enabled/0]).
-spec is_debug_mode_enabled() -> boolean().
-ifdef(debug_mode_is_enabled).
is_debug_mode_enabled() ->
true.
-else.
is_debug_mode_enabled() ->
false.
-endif.
"""
(another example could be the use of a random source based on crypto - if
available, otherwise on the built-in source)
Dialyzer is right when telling that "is_debug_mode_enabled/0 states that
the function might also return 'false' but the inferred return is 'true'"
(when the symbol debug_mode_is_enabled is defined) , but I would like to
be able to suppress this warning as the same code built with a different
preprocessor flag should offer the same API/type spec to the user code.
Of course I could define instead:
"""
-ifdef(debug_mode_is_enabled).
-spec is_debug_mode_enabled() -> true.
is_debug_mode_enabled() ->
true.
-else.
-spec is_debug_mode_enabled() -> false.
is_debug_mode_enabled() ->
false.
-endif.
"""
but then, I think it would not really solve the problem as I suppose that
Dialyzer would similarly complain then in the user code relying on these
functions, like in:
"""
-module(bar).
-spec f() -> boolean()
f() ->
foo:is_debug_mode_enabled().
"""
(the problem would then be transferred from foo to bar and all other
places making use of foo)
Am I missing something? Is there a way to tell Dialyzer to ignore *for
this particular function* the "underspecification"?
If not, maybe making Dialyzer support Valgrind-like suppression files
could help?
Thanks in advance for any hint,
Best regards,
Olivier.
---------------------------
Olivier Boudeville
EDF R&D : 1, avenue du Général de Gaulle, 92140 Clamart, France
Département SINETICS, groupe ASICS (I2A), bureau B-226
Office : +33 1 47 65 59 58 / Mobile : +33 6 16 83 37 22 / Fax : +33 1 47
65 27 13
Ce message et toutes les pièces jointes (ci-après le 'Message') sont établis à l'intention exclusive des destinataires et les informations qui y figurent sont strictement confidentielles. Toute utilisation de ce Message non conforme à sa destination, toute diffusion ou toute publication totale ou partielle, est interdite sauf autorisation expresse.
Si vous n'êtes pas le destinataire de ce Message, il vous est interdit de le copier, de le faire suivre, de le divulguer ou d'en utiliser tout ou partie. Si vous avez reçu ce Message par erreur, merci de le supprimer de votre système, ainsi que toutes ses copies, et de n'en garder aucune trace sur quelque support que ce soit. Nous vous remercions également d'en avertir immédiatement l'expéditeur par retour du message.
Il est impossible de garantir que les communications par messagerie électronique arrivent en temps utile, sont sécurisées ou dénuées de toute erreur ou virus.
____________________________________________________
This message and any attachments (the 'Message') are intended solely for the addressees. The information contained in this Message is confidential. Any use of information contained in this Message not in accord with its purpose, any dissemination or disclosure, either whole or partial, is prohibited except formal approval.
If you are not the addressee, you may not copy, forward, disclose or use any part of it. If you have received this message in error, please delete it and all copies from your system and notify the sender immediately by return message.
E-mail communication cannot be guaranteed to be timely secure, error or virus-free.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20111118/ca097b45/attachment.htm>
More information about the erlang-questions
mailing list