On this page:
3.10.1 The String  Dict Type
String  Dict
3.10.2 String  Dict Constructor
string-dict
3.10.3 String  Dict Methods
.get
.get-value
.set
.has-key
.keys
.remove
.count
.unfreeze
3.10.4 The Mutable  String  Dict Type
Mutable  String  Dict
3.10.5 Mutable  String  Dict Constructor
mutable-string-dict
3.10.6 Mutable  String  Dict Methods
.get-now
.get-value-now
.set-now
.has-key-now
.keys-now
.remove-now
.count-now
.freeze
.seal

3.10 string-dict

Usage:
include string-dict
import string-dict as ...

3.10.1 The StringDict Type

StringDicts keep track of a mapping from Strings to any Pyret value. A StringDict is immutable, i.e., a mapping cannot be changed; however, a new StringDict can be fashioned from an existing StringDict with a new, omitted, or changed mapping.

A StringDict can be unfrozen, i.e., used as the basis for MutableStringDict, which does allow modification (see below).

There are no variants for StringDicts, and programs cannot use cases statements with StringDicts. Instead, they can be created with the constructors below, and manipulated with the methods and functions below.

3.10.2 StringDict Constructor
[string-dict: key :: String, elt :: a, ...] -> StringDict<a>

Creates a string-dict with the given elts.

Examples:

sd1 = [string-dict: "a", 5, "b", 10]

3.10.3 StringDict Methods

.get :: (key :: String) -> Option<a>

Returns none if the key is not in the dictionary, and a some containing the value the key maps to if the key is in the dictionary.

Examples:

check: [string-dict: "a", 5].get("a") is some(5) [string-dict: "a", 5].get("b") is none end

.get-value :: (key :: String) -> a

Returns the value that key maps to if it is present, and throws an exception otherwise.

Examples:

check: [string-dict: "a", 5].get-value("a") is 5 [string-dict: "a", 5].get-value("b") raises "Key b not found" end

.set :: (key :: String, value :: a) -> StringDict<a>

Returns a new string-dict that maps key to value and is otherwise similar to the original string-dict.

Examples:

check: sd1 = [string-dict: "a", 5, "b", 10] sd1.get-value("a") is 5 sd1.get-value("b") is 10 sd2 = sd1.set("a", 15) sd2.get-value("a") is 15 sd2.get-value("b") is 10 end

.has-key :: (key :: String) -> Boolean

Returns true if key is in the string-dict; false if not.

Examples:

check: sd1 = [string-dict: "a", 5] sd1.has-key("a") is true sd1.has-key("b") is false end

.keys :: () -> Set<String>

Returns the set of keys in the string-dict.

Examples:

check: sd1 = [string-dict: "a", 5, "b", 10] sd1.keys() is [tree-set: "a", "b"] sd1.keys() is [tree-set: "b", "a"] end

.remove :: (key :: String) -> StringDict<a>

Returns a new string-dict that doesn’t have the argument key but is otherwise similar to the original string-dict.

Examples:

check: sd1 = [string-dict: "a", 5, "b", 10] sd1.has-key("a") is true sd1.has-key("b") is true sd2 = sd1.remove("b") sd2.has-key("a") is true sd2.has-key("b") is false end

.count :: () -> Number

Returns the number of keys in the string-dict.

Examples:

check: sd1 = [string-dict: "a", 5, "b", 10] sd1.count() is 2 sd2 = sd1.set("c", 15) sd2.count() is 3 sd3 = sd1.remove("a") sd3.count() is 1 end

.unfreeze :: () -> MutableStringDict<a>

Returns a mutable string-dict that has the same keys and values as the original string-dict.

Examples:

check: sd1 = [string-dict: "a", 5, "b", 10] msd1 = sd1.unfreeze() msd1.set-now("a", 0) msd1.get-value-now("a") is 0 end

3.10.4 The MutableStringDict Type

MutableStringDicts keep track of a mapping from Strings to any Pyret value. In contrast to StringDicts, a MutableStringDict can have mappings added, deleted, or changed.

A MutableStringDict can be sealed to produce a variant that is read-only. A MutableStringDict can also be frozen to produce an immutable StringDict (see above).

There are no variants for MutableStringDicts, and programs cannot use cases statements with MutableStringDicts. Instead, they can be created with the constructors below, and manipulated with the methods and functions below.

3.10.5 MutableStringDict Constructor
[mutable-string-dict: elt :: a, ...] -> MutableStringDict<a>

Creates an mutable string-dict with the given elts.

Examples:

msd1 = [mutable-string-dict: "a", 5, "b", 10]

3.10.6 MutableStringDict Methods

.get-now :: (key :: String) -> Option<a>

Returns none if the key is not in the dictionary, and a some containing the value the key maps to if the key is in the dictionary.

Examples:

check: [mutable-string-dict: "a", 5].get-now("a") is some(5) [mutable-string-dict: "a", 5].get-now("b") is none end

.get-value-now :: (key :: String) -> a

Returns the value that key maps to if it is present, and throws an exception otherwise.

Examples:

check: [mutable-string-dict: "a", 5].get-value-now("a") is 5 [mutable-string-dict: "a", 5].get-value-now("b") raises "Key b not found" end

.set-now :: (key :: String, value :: a) -> Nothing

Modifies the mutable-string-dict so that it now maps key to value. This method is called only for its side-effect and so returns nothing

Examples:

check: msd1 = [mutable-string-dict: "a", 5, "b", 10] msd1.get-value-now("a") is 5 msd1.get-value-now("b") is 10 msd1.set-now("a", 15) is nothing msd1.get-value-now("a") is 15 msd1.get-value-now("b") is 10 end

.has-key-now :: (key :: String) -> Boolean

Returns true if key is in the string-dict; false if not.

Examples:

check: msd1 = [mutable-string-dict: "a", 5] msd1.has-key-now("a") is true msd1.has-key-now("b") is false end

.keys-now :: () -> Set<String>

Returns the set of keys in the string-dict.

Examples:

check: msd1 = [mutable-string-dict: "a", 5, "b", 10] msd1.keys-now() is [tree-set: "a", "b"] msd1.keys-now() is [tree-set: "b", "a"] end

.remove-now :: (key :: String) -> Nothing

Modifies the mutable-string-dict so that it no longer has the argument key.

Examples:

check: msd1 = [mutable-string-dict: "a", 5, "b", 10] msd1.has-key-now("a") is true msd1.has-key-now("b") is true msd1.remove-now("b") is nothing msd1.has-key-now("a") is true msd1.has-key-now("b") is false end

.count-now :: () -> Number

Returns the number of keys in the mutable-string-dict.

Examples:

check: msd1 = [mutable-string-dict: "a", 5, "b", 10] msd1.count-now() is 2 msd1.set-now("c", 15) msd1.count-now() is 3 msd1.remove-now("a") msd1.count-now() is 2 end

.freeze :: () -> StringDict<a>

Returns an immutable string-dict that has the same keys and values as the mutable one.

Examples:

check: msd1 = [mutable-string-dict: "a", 5, "b", 10] sd1 = msd1.freeze() sd2 = sd1.set("a", 10) sd2.get-value("a") is 10 sd1.get-value("a") is 5 end

.seal :: () -> MutableStringDict<a>

Returns a sealed version of the mutable-string-dict that has the same keys and values, but does not allow modification. The original mutable-string-dict continues to be modifiable, and such modifications will be visible in the sealed one.

Examples:

check: msd1 = [mutable-string-dict: "a", 5, "b", 10] smsd1 = msd1.seal() smsd1.get-value-now("a") is 5 smsd1.set-now("a", 15) raises "Cannot modify sealed string dict" msd1.set-now("a", 15) is nothing msd1.get-value-now("a") is 15 smsd1.get-value-now("a") is 15 end