[erlang-questions] Compiler to warn on used underscore-variables
Zoltan Lajos Kis
kiszl@REDACTED
Tue Jan 5 09:47:44 CET 2010
> 2010/1/5 Zoltan Lajos Kis <kiszl@REDACTED>
>
>> I also understand that many compiler warnings (unused variable, wrong
>> no.
>> of args in format call, etc.) are output because there is a clear sign
>> of
>> a (semantic) mistake and not because of the source code being invalid.
>> So I believe that the current behavior of not warning about a most
>> probable mistake with _Var variables is the counter-intuitive one.
>>
>
> Along with single assignment there exists a whole class of errors where
> you
> may mistakenly use a previously bound variable in a pattern intending to
> use
> an unbound one. This has nothing to do with a '_' prefix on them at all,
> rather it's simply a class of errors that exist with single assignment and
> pattern matching.
>
> There is no general solution to this problem either, any more than there
> is
> a general solution to accidently destructively rebinding a variable then
> assuming you were referencing a previous version in languages that support
> destructive assignment.
>
> I'm not sure what alternative you're proposing. Would you prefer all
> variables prefixed with an underscore have the same semantics as '_'?
>
> No compiler is going to be able to think for you.
>
> --
> Tony Arcieri
> Medioh! A Kudelski Brand
>
I'm simply proposing that if the compiler sees code like:
measure_secs(Fun) ->
{_M, S1, _U} = erlang:now(),
Fun(),
{_M, S2, _U} = erlang:now(),
S2 - S1.
to output something like: "Warning: variable '_U' is not unused".
Zoltan.
More information about the erlang-questions
mailing list