[erlang-questions] Reassigning variables

Matthew Dempsky matthew@REDACTED
Tue Mar 17 19:02:20 CET 2009


I like pattern matching in the majority of cases, but I find I write
enough code where I need to incrementally update a data structure, and
maintaining that code is a pain when I have code like:

  X = foo(),
  X1 = bar(X),
  X2 = xyzzy(X1),
  blah(X2).

and later want to change it to:

  X = foo(),
  X1 = whee(X),
  X2 = bar(X1),
  X3 = xyzzy(X2),
  blah(X3).

This means having to change four lines of code, when really it's
conceptually just one change.  I'd like to suggest being able to do
something like:

  X = foo(),
  r(X) = whee(X),
  r(X) = bar(X),
  r(X) = xyzzy(X),
  blah(X).

where "r(?VAR) = ?EXPR" means to reassign ?VAR to ?EXPR, even if it
previously has an assigned value.

Thoughts?  Does anyone have suggestions for better syntax?  I think
this can be handled with a parse transform, and I'm considering
writing a parse transform to handle it.  (I haven't checked if anyone
else has proposed similar functionality in the past, but this is
something that's been bugging me for a while, and I've finally had to
rename variables manually enough times to propose this.)



More information about the erlang-questions mailing list