Skip to content

Commit f31235e

Browse files
committed
Docs ready for v0.8
1 parent 3482f30 commit f31235e

File tree

14 files changed

+434
-277
lines changed

14 files changed

+434
-277
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
- The `string` module gains `is_empty`, `join` and `concat` functions.
1212
- The `int` module gains `is_even` and `is_odd` functions.
1313
- The `list.length` function now accepts a labelled argument.
14+
- The `list.length` function now accepts a labelled argument.
15+
- The the second argument of `bool.compare`, `float.compare`, `int.compare`,
16+
and `order.compare` now have the label `with`.
17+
- The `dynamic.unsafe_coerce` function now only accepts Dynamic data.
18+
- The `dynamic` decoder functions no longer print the entire value in their
19+
error messages, to avoid large errors.
1420

1521
## v0.7.0 - 2020-03-03
1622

gen/test/gleam@dynamic_test.erl

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/gleam/atom.gleam

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ pub type FromStringError {
3434
/// > from_string("some_new_atom")
3535
/// Error(AtomNotLoaded)
3636
///
37-
///
3837
pub external fn from_string(String) -> Result(Atom, FromStringError) =
3938
"gleam_stdlib" "atom_from_string";
4039

src/gleam/bool.gleam

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@ import gleam/order.{Order}
1111
pub type Bool =
1212
Bool
1313

14-
/// Returns the opposite Bool value
14+
/// Returns the opposite bool value.
15+
///
16+
/// This is the same as the `!` or `not` operators in some other languages.
1517
///
1618
/// ## Examples
19+
///
1720
/// > negate(True)
1821
/// False
1922
///
23+
/// > negate(False)
24+
/// True
2025
///
2126
pub fn negate(bool: Bool) -> Bool {
2227
case bool {
@@ -28,12 +33,12 @@ pub fn negate(bool: Bool) -> Bool {
2833
/// Compares two bools and returns the first values Order to the second.
2934
///
3035
/// ## Examples
31-
/// import gleam/order
36+
///
37+
/// > import gleam/order
3238
/// > compare(True, False)
3339
/// order.Gt
3440
///
35-
///
36-
pub fn compare(a: Bool, b: Bool) -> Order {
41+
pub fn compare(a: Bool, with b: Bool) -> Order {
3742
case a, b {
3843
True, True -> order.Eq
3944
True, False -> order.Gt
@@ -42,9 +47,10 @@ pub fn compare(a: Bool, b: Bool) -> Order {
4247
}
4348
}
4449

45-
/// Returns `True` if either Bool value is `True`.
50+
/// Returns True if either bool value is True.
4651
///
4752
/// ## Examples
53+
///
4854
/// > max(True, False)
4955
/// True
5056
///
@@ -54,17 +60,17 @@ pub fn compare(a: Bool, b: Bool) -> Order {
5460
/// > max(False, False)
5561
/// False
5662
///
57-
///
5863
pub fn max(a: Bool, b: Bool) -> Bool {
5964
case a {
6065
True -> True
6166
False -> b
6267
}
6368
}
6469

65-
/// Returns `False` if either Bool value is `False`.
70+
/// Returns False if either bool value is False.
6671
///
6772
/// ## Examples
73+
///
6874
/// > max(True, False)
6975
/// False
7076
///
@@ -74,24 +80,23 @@ pub fn max(a: Bool, b: Bool) -> Bool {
7480
/// > max(False, False)
7581
/// False
7682
///
77-
///
7883
pub fn min(a: Bool, b: Bool) -> Bool {
7984
case a {
8085
False -> False
8186
True -> b
8287
}
8388
}
8489

85-
/// Returns a numeric representation of the value:
90+
/// Returns a numeric representation of the given bool.
8691
///
8792
/// ## Examples
93+
///
8894
/// > to_int(True)
8995
/// 1
9096
///
9197
/// > to_int(False)
9298
/// 0
9399
///
94-
///
95100
pub fn to_int(bool: Bool) -> Int {
96101
case bool {
97102
False -> 0

src/gleam/dynamic.gleam

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ pub external fn from(a) -> Dynamic = "gleam_stdlib" "identity";
1414
/// Unsafely cast a Dynamic value into any other type.
1515
///
1616
/// 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!
1818
///
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";
2022

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.
2225
///
2326
/// ## Examples
27+
///
2428
/// > string(from("Hello"))
2529
/// Ok("Hello")
2630
///
@@ -30,9 +34,11 @@ pub external fn unsafe_coerce(a) -> b = "gleam_stdlib" "identity";
3034
pub external fn string(from: Dynamic) -> Result(String, String)
3135
= "gleam_stdlib" "decode_string"
3236

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.
3439
///
3540
/// ## Examples
41+
///
3642
/// > int(from(123))
3743
/// Ok(123)
3844
///
@@ -42,9 +48,11 @@ pub external fn string(from: Dynamic) -> Result(String, String)
4248
pub external fn int(from: Dynamic) -> Result(Int, String)
4349
= "gleam_stdlib" "decode_int"
4450

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.
4653
///
4754
/// ## Examples
55+
///
4856
/// > float(from(2.0))
4957
/// Ok(2.0)
5058
///
@@ -54,10 +62,12 @@ pub external fn int(from: Dynamic) -> Result(Int, String)
5462
pub external fn float(from: Dynamic) -> Result(Float, String)
5563
= "gleam_stdlib" "decode_float"
5664

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.
5867
///
5968
/// ## Examples
60-
/// import gleam/atom
69+
///
70+
/// > import gleam/atom
6171
/// > atom(from(atom.create_from_string("hello")))
6272
/// OK("hello")
6373
///
@@ -67,9 +77,11 @@ pub external fn float(from: Dynamic) -> Result(Float, String)
6777
pub external fn atom(from: Dynamic) -> Result(atom.Atom, String)
6878
= "gleam_stdlib" "decode_atom"
6979

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.
7182
///
7283
/// ## Examples
84+
///
7385
/// > bool(from(True))
7486
/// Ok(True)
7587
///
@@ -79,11 +91,13 @@ pub external fn atom(from: Dynamic) -> Result(atom.Atom, String)
7991
pub external fn bool(from: Dynamic) -> Result(Bool, String)
8092
= "gleam_stdlib" "decode_bool"
8193

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.
8396
///
8497
/// ## Examples
85-
/// import gleam/result
86-
/// let f = fn() { 1 }
98+
///
99+
/// > import gleam/result
100+
/// > let f = fn() { 1 }
87101
/// > thunk(from(f)) |> result.is_ok
88102
/// True
89103
///
@@ -96,14 +110,19 @@ pub external fn thunk(from: Dynamic) -> Result(fn() -> Dynamic, String)
96110
external fn list_dynamic(from: Dynamic) -> Result(List(Dynamic), String)
97111
= "gleam_stdlib" "decode_list"
98112

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.
100115
///
101116
/// ## Examples
117+
///
102118
/// > list(from(["a", "b", "c"]), string)
103119
/// Ok(["a", "b", "c"])
104120
///
105121
/// > 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")
107126
///
108127
pub fn list(
109128
from dynamic: Dynamic,
@@ -114,28 +133,36 @@ pub fn list(
114133
|> result.then(_, list_mod.traverse(_, decoder_type))
115134
}
116135

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+
///
118139
/// This will not succeed on a record.
119140
///
120141
/// ## Examples
121-
/// import gleam/map
142+
///
143+
/// > import gleam/map
122144
/// > field(from(map.new("Hello", "World")), "Hello")
123145
/// Ok(Dynamic)
124146
///
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")
127149
///
128150
pub external fn field(from: Dynamic, named: a) -> Result(Dynamic, String)
129151
= "gleam_stdlib" "decode_field"
130152

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.
132155
///
133156
/// ## Examples
157+
///
134158
/// > element(from(tuple(1, 2)), 0)
135159
/// Ok(Dynamic)
136160
///
137161
/// > 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")
139166
///
140167
pub external fn element(from: Dynamic, position: Int) -> Result(Dynamic, String)
141168
= "gleam_stdlib" "decode_element"

0 commit comments

Comments
 (0)