[erlang-questions] void() in type specs

Olivier BOUDEVILLE <>
Mon Nov 21 13:39:42 CET 2011


Hi,

Thanks Kostis for your answer. I was just thinking that if the developer 
could provide enough information, the compiler and/or runtime could 
determine that the returned result (here an atom) is not used, and thus 
maybe optimise it out. One step further is allowing the function developer 
not even to bother ending each clause of this function with such an atom, 
as anyway this result would be ignored.

Admittedly it is premature optimisation, but on the other hand having to 
return *explicitly* a value that is known never to be used is a bit weird!

I think I will try to stick to '-opaque void() :: any().', trusting user 
code not to mess with return values (and catching any attempt of doing so 
thanks to Dialyzer), and will fall back to something like: '-opaque 
'void() :: 'VoiD'.' (and thus adding the returning of this atom everywhere 
needed) if the first approach had drawbacks I did not anticipate.

Thanks again!
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



 
Envoyé par : 
18/11/2011 20:12

A

cc

Objet
Re: [erlang-questions] void() in type specs






On 11/18/11 15:25, Olivier BOUDEVILLE wrote:
>
> Hi,
>
> How can we define a type spec for a function which is useful only for
> its side effects (not returning anything of interest to the caller)?
>
> In the documentation of various modules there are functions that are
> said to return 'void()' (ex: erlang:purge_module/1), however the
> compiler complains if I want to use it in my code ("type void()
> undefined") and void() is not mentioned in
> http://www.erlang.org/doc/reference_manual/typespec.html
>
> I imagine we could define such a "don't care"-type with a '-opaque
> void() :: any().' but wouldn't it be more practical if it was a built-in
> keyword? Moreover it could allow Dialyzer to check that the result is
> indeed never used (or maybe I overlooked something?)

There is no void() in Erlang and functions that return always return 
some value. To my mind, the only sensible values for don't care returns 
are atoms. The atoms 'ok' or 'void' are among the best such values you 
can get. Whether callers are supposed to check or depend on this value 
or not is a different matter. If you don't care, simply write these 
functions as follows:

foo(...) ->
    ... DO LOTS OF SIDE-EFFECTS HERE ...,
    ... MORE SIDE EFFECTS ...,
    ok.

and spec them as:

-spec foo(...SOME TYPES...) -> 'ok'.

or if you prefer,

-type void() :: 'ok'.
-spec foo(...SOME TYPES...) -> void().


If you absolutely want to guarantee that there is no pattern matching 
with this void(), choose some weird atom for their return (eg. 'VoiD') 
and then declare this return value opaque:

-opaque void() :: 'VoiD'.

Dialyzer will then warn you for all cases that you trying to explicitly 
pattern match with this return.

But, out of curiosity, why guaranteeing that there is no matching with 
whatever happens to be used for void() is so important to you?


Kostis
_______________________________________________
erlang-questions mailing list

http://erlang.org/mailman/listinfo/erlang-questions




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/20111121/2a1d8f06/attachment.html>


More information about the erlang-questions mailing list