[erlang-questions] Simple Erlang Recommendation (last returned value)

Richard A. O'Keefe ok@REDACTED
Mon Jul 28 00:40:12 CEST 2008

On 26 Jul 2008, at 4:14 am, Berlin Brown wrote:

> Maybe this is already implemented, but I wish there were some kind of
> "last_value" built in function where you can get the last value
> without having to define it yourself.

But what _is_ the "last value"?
I can make sense of such a thing at the top interactive level,
but not elsewhere.
> Has this been talked about a lot already.
> E.g. this is from the erlang docs as a "best" practice.
> NewQ = queue:new(),
> Queue1 = queue:add(joe, NewQ),
> Queue2 = queue:add(mike, Queue1), ...
> When I am coding, I end up forgetting to define Q1, Q2
> NewQ = queue:new(),
> queue:add(joe, NewQ),
> queue:add(mike, NewQ), ...

Then learn to do better.
For one thing, this particular example should be
	Q = queue:from_list([joe, mike])

> I wish there were something like this to get the result of the last
> call
> queue:new(),
> queue:add(joe,  erlang:last_value()),
> queue:add(mike, erlang:last_value())), ...

(A) There is: it's called binding a variable.
(B) If you are still thinking imperatively, so that you expect a
     function to work by side effects rather than by returning a
     new value, what makes you think that you will remember to
     use erlang:last_value() instead of NewQ?

It is really very common for the thing you want to pass
here NOT to be the result of the last value, so the proposed
hack really would not help except in trivial cases.

More information about the erlang-questions mailing list