e r l a n g : c o o k b o o k

/ HashRecipeAddingElement / Erlang.HashRecipeImmutable

This Web


WebHome 
WebChanges 
TOC
NewRecipe 
WebTopicList 
WebStatistics 

All Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Erlang Links


Erlang.org
Erlang Wiki
ErlMan
Erlang Wiki
The Jungerl
Erlang-fr.org
Joe Armstrong
Lambda the Ultimate

Erlang Web Ring


[Prev]: Joe Armstrong's Page
[Next]: Joe Armstrong's Page

Creating a Hash with Immutable Keys or Values

Problem

You would like to have a hash table whose values or keys couldn't be modified once set.

Solution

In some ways, Erlang dict Dictionaries are immutable by design. Each time you insert a value into a dictionary, it creates a brand new dictionary containing the data from the old dictionary, plus the newly inserted entry.

If you wish to have a read-only dictionary, you can structure your program so that the dict term is passed into functions for use, but that you never receive a new dictionary value from another function. In this way, the single-assignment nature of Erlang guarantees that the dictionary is unchanged.

ets tables are more problematic, because the permit updates to the table by name, and Erlang's single-assigment behavior does not provide any protection here.

Discussion

This seems to be an area where Erlang can only provide this functionality if the programmer uses the single-assignment nature of the language to proper use.


Comments about this recipe

Contributors

Based on work by KarlaRamirez.

-- BrentAFulgham - 31 Aug 2004

CookbookForm
TopicType: Recipe
ParentTopic: HashRecipes
TopicOrder: 040

 
 
Copyright © 2004 by the contributing authors. All material on the Erlang Cookbook web site is the property of the contributing authors.
This material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest