@@ -14,13 +14,17 @@ pub external fn from(a) -> Dynamic = "gleam_stdlib" "identity";
14
14
/// Unsafely cast a Dynamic value into any other type.
15
15
///
16
16
/// This is an escape hatch for the type system that may be useful when wrapping
17
- /// native Erlang APIs. It is to be used as a last measure only.
17
+ /// native Erlang APIs. It is to be used as a last measure only!
18
18
///
19
- pub external fn unsafe_coerce(a) -> b = "gleam_stdlib" "identity";
19
+ /// If you can avoid using this function, do!
20
+ ///
21
+ pub external fn unsafe_coerce(Dynamic) -> a = "gleam_stdlib" "identity";
20
22
21
- /// Safely cast a Dynamic value into a String.
23
+ /// Check to see whether a Dynamic value is a string, and return the string if
24
+ /// it is.
22
25
///
23
26
/// ## Examples
27
+ ///
24
28
/// > string(from("Hello"))
25
29
/// Ok("Hello")
26
30
///
@@ -30,9 +34,11 @@ pub external fn unsafe_coerce(a) -> b = "gleam_stdlib" "identity";
30
34
pub external fn string(from: Dynamic) -> Result(String, String)
31
35
= "gleam_stdlib" "decode_string"
32
36
33
- /// Safely cast a Dynamic value into a String.
37
+ /// Check to see whether a Dynamic value is an int, and return the int if it
38
+ /// is.
34
39
///
35
40
/// ## Examples
41
+ ///
36
42
/// > int(from(123))
37
43
/// Ok(123)
38
44
///
@@ -42,9 +48,11 @@ pub external fn string(from: Dynamic) -> Result(String, String)
42
48
pub external fn int(from: Dynamic) -> Result(Int, String)
43
49
= "gleam_stdlib" "decode_int"
44
50
45
- /// Safely cast a Dynamic value into a Float.
51
+ /// Check to see whether a Dynamic value is an float, and return the float if
52
+ /// it is.
46
53
///
47
54
/// ## Examples
55
+ ///
48
56
/// > float(from(2.0))
49
57
/// Ok(2.0)
50
58
///
@@ -54,10 +62,12 @@ pub external fn int(from: Dynamic) -> Result(Int, String)
54
62
pub external fn float(from: Dynamic) -> Result(Float, String)
55
63
= "gleam_stdlib" "decode_float"
56
64
57
- /// Safely cast a Dynamic value into an Atom.
65
+ /// Check to see whether a Dynamic value is an atom, and return the atom if
66
+ /// it is.
58
67
///
59
68
/// ## Examples
60
- /// import gleam/atom
69
+ ///
70
+ /// > import gleam/atom
61
71
/// > atom(from(atom.create_from_string("hello")))
62
72
/// OK("hello")
63
73
///
@@ -67,9 +77,11 @@ pub external fn float(from: Dynamic) -> Result(Float, String)
67
77
pub external fn atom(from: Dynamic) -> Result(atom.Atom, String)
68
78
= "gleam_stdlib" "decode_atom"
69
79
70
- /// Safely cast a Dynamic value into a Bool.
80
+ /// Check to see whether a Dynamic value is an bool, and return the bool if
81
+ /// it is.
71
82
///
72
83
/// ## Examples
84
+ ///
73
85
/// > bool(from(True))
74
86
/// Ok(True)
75
87
///
@@ -79,11 +91,13 @@ pub external fn atom(from: Dynamic) -> Result(atom.Atom, String)
79
91
pub external fn bool(from: Dynamic) -> Result(Bool, String)
80
92
= "gleam_stdlib" "decode_bool"
81
93
82
- /// Safely cast a Dynamic value into a String.
94
+ /// Check to see whether a Dynamic value is a function that takes no arguments,
95
+ /// and return the function if it is.
83
96
///
84
97
/// ## Examples
85
- /// import gleam/result
86
- /// let f = fn() { 1 }
98
+ ///
99
+ /// > import gleam/result
100
+ /// > let f = fn() { 1 }
87
101
/// > thunk(from(f)) |> result.is_ok
88
102
/// True
89
103
///
@@ -96,14 +110,19 @@ pub external fn thunk(from: Dynamic) -> Result(fn() -> Dynamic, String)
96
110
external fn list_dynamic(from: Dynamic) -> Result(List(Dynamic), String)
97
111
= "gleam_stdlib" "decode_list"
98
112
99
- /// Safely cast a Dynamic value into a List of some type.
113
+ /// Check to see whether a Dynamic value is a list, and return the list if it
114
+ /// is.
100
115
///
101
116
/// ## Examples
117
+ ///
102
118
/// > list(from(["a", "b", "c"]), string)
103
119
/// Ok(["a", "b", "c"])
104
120
///
105
121
/// > list(from([1, 2, 3]), string)
106
- /// Error("Expected a List, got `[1, 2, 3]`")
122
+ /// Error("Expected an Int, got a binary")
123
+ ///
124
+ /// > list(from("ok"), string)
125
+ /// Error("Expected a List, got a binary")
107
126
///
108
127
pub fn list(
109
128
from dynamic: Dynamic,
@@ -114,28 +133,36 @@ pub fn list(
114
133
|> result.then(_, list_mod.traverse(_, decoder_type))
115
134
}
116
135
117
- /// Returns a field from a Dynamic value representing a map if it exists.
136
+ /// Check to see if a Dynamic value is a map with a specific field, and return
137
+ /// the value of the field if it is.
138
+ ///
118
139
/// This will not succeed on a record.
119
140
///
120
141
/// ## Examples
121
- /// import gleam/map
142
+ ///
143
+ /// > import gleam/map
122
144
/// > field(from(map.new("Hello", "World")), "Hello")
123
145
/// Ok(Dynamic)
124
146
///
125
- /// > field(from(123))
126
- /// Error("Expected a map with key `\"Hello\"`, got `123` ")
147
+ /// > field(from(123), "Hello" )
148
+ /// Error("Expected a map with key `\"Hello\"`, got an Int ")
127
149
///
128
150
pub external fn field(from: Dynamic, named: a) -> Result(Dynamic, String)
129
151
= "gleam_stdlib" "decode_field"
130
152
131
- /// Returns an element of a Dynamic value representing a tuple if it exists.
153
+ /// Check to see if the Dynamic value is a tuple large enough to have a certain
154
+ /// index, and return the value of that index if it is.
132
155
///
133
156
/// ## Examples
157
+ ///
134
158
/// > element(from(tuple(1, 2)), 0)
135
159
/// Ok(Dynamic)
136
160
///
137
161
/// > element(from(tuple(1, 2)), 2)
138
- /// Error("Element position is out-of-bounds")
162
+ /// Error("Expected a tuple of at least 3 size, got a tuple of 2 size")
163
+ ///
164
+ /// > element(from(""), 2)
165
+ /// Error("Expected a Tuple, got a binary")
139
166
///
140
167
pub external fn element(from: Dynamic, position: Int) -> Result(Dynamic, String)
141
168
= "gleam_stdlib" "decode_element"
0 commit comments