[erlang-questions] Reassigning variables

Pete Bergstrom bergstro@REDACTED
Tue Mar 17 19:36:03 CET 2009


"FAQ 5.6 ...destructively update data, like in C

Not being able to do this is considered a feature of Erlang. The Erlang book
explains this in chapter 1.

Having said that, there are common methods to achieve effects similar to
destructive update: store the data in a process (use messages to manipulate
it), store the data in ETS or use a data structure designed for relatively
cheap updates (the dict library module is one common solution). "

------ Original Message ------
Received: Tue, 17 Mar 2009 01:13:56 PM CDT
From: Matthew Dempsky <matthew@REDACTED>
To: Erlang Questions <erlang-questions@REDACTED>
Subject: [erlang-questions] Reassigning variables

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),

and later want to change it to:

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

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),

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.)
erlang-questions mailing list

More information about the erlang-questions mailing list