[erlang-questions] How about a new warning? Was: Re: trouble with erlang or erlang is a ghetto

Richard O'Keefe <>
Mon Aug 1 02:05:09 CEST 2011

On 29/07/2011, at 9:30 PM, Tim Watson wrote:

> On 29 July 2011 00:45, Richard O'Keefe <> wrote:
>> One of the things criticised in the blog entry that we've been responding to was
>> that
>>        {ok,Foo} = bar(...),
>>        {ok,Foo} = ugh(...)
>> is too easy to write (when you really meant, say, Foo0, Foo1).
>> This is a well defined part of the language, and it would not be a good idea to
>> ban it.  But how about an optional style warning (and we really need
>>        -warn(on | off, [warning...])
>> directives) whenever a bound variable appears in a pattern on the left of "="?
> We would need to be able to set that warning with a very localised
> scope though. What you are intending in such a match might be
> *exactly* what the code says - call bar/1 to get a Foo and then call
> ugh/2 and match (assert) that the resulting tuple contains exactly the
> same Foo. Things like a session id, (ets) table id and so on are
> probably examples of this. I won't comment on whether this is good API
> design or not.

There is another way to express that intention that does not require
the use of this anti-pattern.

    crash_if_different(X, X) -> ok.

    ... ->
	{ok,Bar_Foo} = bar(...),
	{ok,Ugh_Foo} = ugh(...),
	crash_if_different(Bar_Foo, Ugh_Foo),

> A -warn(on | off, [Warning]) sounds lovely, but would only work at
> module level (or as some option passed in to the compiler)

Where did that restriction come from?
Not from anything *I* wrote.
I was imitating Prolog style warnings, which take effect from the
point where they are asserted to the point where they are next set.
For example,
	:- style_check(+charset).
	:- style_check(-charset).
gets a non-portable characters warning for the first clause of p/1
but not for the second clause.

> so how
> would I do (or override) this for an individual line of code or for a
> single function?

Switch the warning on (or off) just before the function,
then switch it off (or on) again just after.

As for single lines of code, crash_if_different/2 means there is no
use case for such an override.

> What about function level attributes? That might get
> the scope at which the switch is applied tight enough to be useful:
> -pragma(no_warnings, [bound_variable_in_match]).

This is exactly what I proposed, just spelled differently.
I don't think the spelling of the style check directives matters much,
and have no objection to using -pragma here.

> fwibble_ardvarks() ->
>    {ok,Foo} = bar(...),
>    {ok,Foo} = ugh(...),
>    %% etc....
>    Foo.

More information about the erlang-questions mailing list