[erlang-questions] void() in type specs
Kostis Sagonas
kostis@REDACTED
Mon Nov 21 20:43:00 CET 2011
On 11/21/11 14:39, Olivier BOUDEVILLE wrote:
>
> 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!
Returning a value (esp. an atomic one, like an atom or an integer) is
really really cheap. There is really not much that the implementation
can save by not returning the value. If the return value of a call is
not matched (at the place of where the call appears) the compiler will
optimize this by not generating any instructions at these call points.
> I think I will try to stick to '-opaque void() :: any().',
Please don't lie in the types. Choose a term for the void type and use
it in the declaration. The opaque declaration is *module local*. This in
any case means that whatever term you choose for void() will not be
visible outside the module that it is declared.
I hope what I write above is clear. You will gain *absolutely nothing*
by declaring the void type as any() instead of the value that is really
used.
> 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.
Kostis
More information about the erlang-questions
mailing list