Skip to content

Instantly share code, notes, and snippets.

@amiller
Created February 22, 2012 04:37
Show Gist options
  • Save amiller/1881453 to your computer and use it in GitHub Desktop.
Save amiller/1881453 to your computer and use it in GitHub Desktop.
-module(mutable).
-export([var/0, get/1, set/2]).
loop(Val) ->
receive
{get, Pid} ->
Pid ! Val,
loop(Val);
{set, V} ->
loop(V)
end.
get(M) ->
M ! {get, self()},
receive
Val -> Val
end.
set(M, V) ->
M ! {set, V},
ok.
var() ->
spawn(fun () -> loop(0) end).
% Example
% c(mutable).
% X = mutable:var().
% mutable:set(X, 15).
% mutable:get(X). % returns 15
% mutable:set(X, "hi").
% mutable.get(X). % returns "hi"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment