Skip to content

Commit 68be9b9

Browse files
committed
Improve classify
1 parent 762161e commit 68be9b9

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
- The `from` function in the `dynamic` module has been deprecated.
77
- The `array`, `bit_array`, `bool`, `float`, `int`, `list`, `nil`, `properties`,
88
and `string` functions have been added to the `dynamic` module.
9+
- The `classify` function in the `dynamic` module now understands more Erlang
10+
types and uses the term "Array" rather than "Tuple" for Erlang tuples and
11+
JavaScript arrays.
912
- The performance of various functions in the `list` module has been improved.
1013
- Fixed the implementation of `option.values` and `option.all` to be tail
1114
recursive.

src/gleam_stdlib.erl

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
string_starts_with/2, wrap_list/1, string_ends_with/2, string_pad/4,
77
uri_parse/1, bit_array_slice/3, percent_encode/1, percent_decode/1,
88
base_decode64/1, parse_query/1, bit_array_concat/1,
9-
bit_array_base64_encode/2, size_of_tuple/1,
10-
tuple_get/2, classify_dynamic/1, print/1, println/1, print_error/1,
11-
println_error/1, inspect/1, float_to_string/1, int_from_base_string/2,
12-
utf_codepoint_list_to_string/1, contains_string/2, crop_string/2,
13-
base16_encode/1, base16_decode/1, string_replace/3, slice/3,
9+
bit_array_base64_encode/2, tuple_get/2, classify_dynamic/1, print/1,
10+
println/1, print_error/1, println_error/1, inspect/1, float_to_string/1,
11+
int_from_base_string/2, utf_codepoint_list_to_string/1, contains_string/2,
12+
crop_string/2, base16_encode/1, base16_decode/1, string_replace/3, slice/3,
1413
bit_array_to_int_and_size/1, bit_array_pad_to_bytes/1, index/2, list/5,
1514
dict/1, int/1, float/1, bit_array/1, is_null/1
1615
]).
@@ -45,6 +44,8 @@ iodata_append(Iodata, String) -> [Iodata, String].
4544
identity(X) -> X.
4645

4746
classify_dynamic(nil) -> <<"Nil">>;
47+
classify_dynamic(null) -> <<"Nil">>;
48+
classify_dynamic(undefined) -> <<"Nil">>;
4849
classify_dynamic(X) when is_boolean(X) -> <<"Bool">>;
4950
classify_dynamic(X) when is_atom(X) -> <<"Atom">>;
5051
classify_dynamic(X) when is_binary(X) -> <<"String">>;
@@ -53,17 +54,17 @@ classify_dynamic(X) when is_integer(X) -> <<"Int">>;
5354
classify_dynamic(X) when is_float(X) -> <<"Float">>;
5455
classify_dynamic(X) when is_list(X) -> <<"List">>;
5556
classify_dynamic(X) when is_map(X) -> <<"Dict">>;
56-
classify_dynamic(X) when is_tuple(X) ->
57-
iolist_to_binary(["Tuple of ", integer_to_list(tuple_size(X)), " elements"]);
57+
classify_dynamic(X) when is_tuple(X) -> <<"Array">>;
58+
classify_dynamic(X) when is_reference(X) -> <<"Reference">>;
59+
classify_dynamic(X) when is_pid(X) -> <<"Pid">>;
60+
classify_dynamic(X) when is_port(X) -> <<"Port">>;
5861
classify_dynamic(X) when
5962
is_function(X, 0) orelse is_function(X, 1) orelse is_function(X, 2) orelse
6063
is_function(X, 3) orelse is_function(X, 4) orelse is_function(X, 5) orelse
6164
is_function(X, 6) orelse is_function(X, 7) orelse is_function(X, 8) orelse
6265
is_function(X, 9) orelse is_function(X, 10) orelse is_function(X, 11) orelse
6366
is_function(X, 12) -> <<"Function">>;
64-
classify_dynamic(_) -> <<"Some other type">>.
65-
66-
size_of_tuple(Data) -> tuple_size(Data).
67+
classify_dynamic(_) -> <<"Unknown">>.
6768

6869
tuple_get(_tup, Index) when Index < 0 -> {error, nil};
6970
tuple_get(Data, Index) when Index >= tuple_size(Data) -> {error, nil};

src/gleam_stdlib.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,11 +634,11 @@ export function classify_dynamic(data) {
634634
} else if (Number.isInteger(data)) {
635635
return "Int";
636636
} else if (Array.isArray(data)) {
637-
return `Tuple of ${data.length} elements`;
637+
return `Array`;
638638
} else if (typeof data === "number") {
639639
return "Float";
640640
} else if (data === null) {
641-
return "Null";
641+
return "Nil";
642642
} else if (data === undefined) {
643643
return "Nil";
644644
} else {

0 commit comments

Comments
 (0)