forget

Kent Boortz kent@REDACTED
Tue May 28 00:32:09 CEST 2002


Vance Shipley <vances@REDACTED> writes:
> Today I was bitten once again by the using the wrong variable.
> 
> foo(Value) ->
> 	Value2 = bar(Value),
> 	Value3 = baz(Value2),
> 	Value4 = zab(Value3),
> 	{ok, Value4}.
> 
> With something like the above I later add another step and everything
> has to be rewritten.  If I forget a step somewhere it compiles and
> runs but the wrong value is used in the place where the code wasn't
> updated correctly.  

Some of these errors are catched using the compiler warning flag for
unused variables. But not all of them. I set the flag with an
environment variable, like

  ERL_COMPILER_OPTIONS=warn_unused_vars

> The idea of having the linter follow a coding style such as used above 
> and warn when a variable version older than the most recently assigned
> is used was discussed on the list some time ago.  That would have 
> prevented me from tripping up today.
> 
> Is that plan any closer to being implemented?

I don't know if I misunderstand what you mean but one idea I have had
is to use something similar to Prolog DCG (definite clause grammar)
syntax so that the code can be written hiding the extra parameter
passed around to every call. For example something like

  foo() -->
         bar(),                   % Call to other --> functions that return
         X = baz(),               % implicitly return {AnyValue,HiddenVar}
         Val = v(),               % Special call v() returns hidden parameter
                                  % at the point of the call.
         c(Value = zab(Val,X)),   % Not a --> call, normal function call
         Value.

could be expanded by the preprocessor to

  foo(S0) ->
         {_,S1} = bar(S0),
         {X,S2} = baz(S1),
         Val = S2,            % or a call {Val,S3} = v(S2) if not special
         Value = zab(Val,X),
         {Value,S2}.          % or {Value,S3} if c/0 not special 

  v(S) -> {S,S}.

But I don't know how much cluttering is removed, if any, for real
programs. Works nice for Prolog but for Erlang it may not be as
useful,

kent



More information about the erlang-questions mailing list