aboutsummaryrefslogtreecommitdiff
path: root/src/atom.gleam
blob: e887a0127e1c5defca83b644f7359e20f126dae2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import expect

pub external type Atom;

pub enum AtomNotLoaded =
  | AtomNotLoaded

pub external fn from_string(String) -> Result(Atom, AtomNotLoaded) =
  "gleam__stdlib" "atom_from_string";

test from_string {
  "ok"
  |> from_string
  |> expect:is_ok

  "expect"
  |> from_string
  |> expect:is_ok

  "this is not an atom we have seen before"
  |> from_string
  |> expect:equal(_, Error(AtomNotLoaded))
}

// This function can create a new atom if one does not already exist for
// the given string. Atoms are not garbage collected so this can result
// in a memory leak if called over time on new values
//
pub external fn create_from_string(String) -> Atom =
  "gleam__stdlib" "atom_create_from_string";

test create_from_string {
  let ok = fn(x) { Ok(x) }

  "ok"
  |> create_from_string
  |> ok
  |> expect:equal(_, from_string("ok"))

  "expect"
  |> create_from_string
  |> ok
  |> expect:equal(_, from_string("expect"))

  "this is another atom we have not seen before"
  |> create_from_string
  |> ok
  |> expect:equal(_, from_string("this is another atom we have not seen before"))
}

pub external fn to_string(Atom) -> String =
  "gleam__stdlib" "atom_to_string";

test to_string {
  "ok"
  |> create_from_string
  |> to_string
  |> expect:equal(_, "ok")

  "expect"
  |> create_from_string
  |> to_string
  |> expect:equal(_, "expect")
}