Skip to content

Commit 28ebbd2

Browse files
authored
fix io.debug to behave the same on Erlang and JavaScript (#308)
1 parent dd5a955 commit 28ebbd2

File tree

4 files changed

+25
-23
lines changed

4 files changed

+25
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- The `string_builder` module loses the `from_float` function. Use `float.to_string` instead.
1212
- Fixed the `int.power` and `float.power` functions by properly handling error cases.
1313
- The grapheme iterator used by `string.graphemes` is now locale independent on target JavaScript.
14+
- Unified `io.debug` to yield Gleam syntax to standard output (stdout) not just on JavaScript but also Erlang.
1415

1516
## v0.21.0 - 2022-04-24
1617

src/gleam/io.gleam

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import gleam/string
2+
13
/// Writes a string to standard output.
24
///
35
/// If you want your output to be printed on its own line see `println`.
@@ -48,7 +50,7 @@ if javascript {
4850
"../gleam_stdlib.mjs" "log"
4951
}
5052

51-
/// Prints a value to standard output using Erlang syntax.
53+
/// Prints a value to standard output (stdout) yielding Gleam syntax.
5254
///
5355
/// The value is returned after being printed so it can be used in pipelines.
5456
///
@@ -73,24 +75,9 @@ if javascript {
7375
/// ```
7476
///
7577
pub fn debug(term: anything) -> anything {
76-
debug_print(term)
7778
term
78-
}
79-
80-
if erlang {
81-
fn debug_print(term: anything) -> DoNotLeak {
82-
erlang_fwrite("~tp\n", [term])
83-
}
84-
}
79+
|> string.inspect
80+
|> println
8581

86-
if javascript {
87-
external fn debug_print(anything) -> Nil =
88-
"../gleam_stdlib.mjs" "debug"
89-
}
90-
91-
if erlang {
92-
external type DoNotLeak
93-
94-
external fn erlang_fwrite(String, List(a)) -> DoNotLeak =
95-
"io" "fwrite"
82+
term
9683
}

src/gleam/string.gleam

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import gleam/list
66
import gleam/option.{None, Option, Some}
77
import gleam/order
88
import gleam/string_builder
9+
import gleam/string_builder.{StringBuilder}
910

1011
if erlang {
1112
import gleam/result
@@ -824,17 +825,19 @@ pub fn capitalise(s: String) -> String {
824825
}
825826
}
826827

827-
pub fn inspect(value: a) -> String {
828-
do_inspect(value)
828+
/// Returns a `String` representation of values in Gleam syntax.
829+
///
830+
pub fn inspect(term: anything) -> String {
831+
do_inspect(term)
829832
|> string_builder.to_string
830833
}
831834

832835
if javascript {
833-
external fn do_inspect(value: a) -> string_builder.StringBuilder =
836+
external fn do_inspect(term: anything) -> StringBuilder =
834837
"../gleam.mjs" "inspect"
835838
}
836839

837840
if erlang {
838-
external fn do_inspect(value: a) -> string_builder.StringBuilder =
841+
external fn do_inspect(term: anything) -> StringBuilder =
839842
"gleam_stdlib" "inspect"
840843
}

test/gleam/io_test.gleam

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
if erlang {
2+
import gleam/io
3+
import gleam/should
4+
5+
pub fn debug_test() {
6+
"io.debug-test"
7+
// prints to stdout, but EUnit will suppress that:
8+
|> io.debug()
9+
|> should.equal("io.debug-test")
10+
}
11+
}

0 commit comments

Comments
 (0)