all: make eprint[ln] behave same as print[ln] (#7595)

pull/7606/head
zakuro 2020-12-27 19:22:16 +09:00 committed by GitHub
parent e69e5c5b91
commit 21805ea2a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 6 deletions

View File

@ -22,13 +22,13 @@ pub fn print(s any) {
} }
pub fn eprintln(s any) { pub fn eprintln(s any) {
JS.console.error(s) JS.console.error(s.toString())
} }
pub fn eprint(s any) { pub fn eprint(s any) {
// TODO // TODO
// $if js.node { // $if js.node {
JS.process.stderr.write(s) JS.process.stderr.write(s.toString())
// } $else { // } $else {
// panic('Cannot `eprint` in a browser, use `eprintln` instead') // panic('Cannot `eprint` in a browser, use `eprintln` instead')
// } // }

View File

@ -1589,8 +1589,8 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type {
unexpected_arguments_pos) unexpected_arguments_pos)
return f.return_type return f.return_type
} }
// println can print anything // println / eprintln can print anything
if fn_name in ['println', 'print'] && call_expr.args.len > 0 { if fn_name in ['println', 'print', 'eprintln', 'eprint'] && call_expr.args.len > 0 {
c.expected_type = table.string_type c.expected_type = table.string_type
call_expr.args[0].typ = c.expr(call_expr.args[0].expr) call_expr.args[0].typ = c.expr(call_expr.args[0].expr)
/* /*

View File

@ -501,8 +501,8 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
} }
} }
mut name := node.name mut name := node.name
is_print := name == 'println' || name == 'print' is_print := name in ['print', 'println', 'eprint', 'eprintln']
print_method := if name == 'println' { 'println' } else { 'print' } print_method := name
is_json_encode := name == 'json.encode' is_json_encode := name == 'json.encode'
is_json_decode := name == 'json.decode' is_json_decode := name == 'json.decode'
g.is_json_fn = is_json_encode || is_json_decode g.is_json_fn = is_json_encode || is_json_decode

View File

@ -1,3 +1,4 @@
fn test_print() { fn test_print() {
println(2.0) println(2.0)
eprintln(2.0)
} }

View File

@ -10,6 +10,8 @@ fn test_autoprint_string_vargs() {
fn add_s(column string, other_columns ...string) { fn add_s(column string, other_columns ...string) {
println(column) println(column)
println(other_columns) println(other_columns)
eprintln(column)
eprintln(other_columns)
} }
// //
@ -23,6 +25,8 @@ fn test_autoprint_int_vargs() {
fn add_i(column int, other_columns ...int) { fn add_i(column int, other_columns ...int) {
println(column) println(column)
println(other_columns) println(other_columns)
eprintln(column)
eprintln(other_columns)
} }
// //
@ -41,4 +45,6 @@ fn test_autoprint_struct_vargs() {
fn add_point(column Point, other_columns ...Point) { fn add_point(column Point, other_columns ...Point) {
println(column) println(column)
println(other_columns) println(other_columns)
eprintln(column)
eprintln(other_columns)
} }