From 304aafdc500b0f391d5ca25576a8e9f28e5a6a06 Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Mon, 21 Dec 2020 20:20:00 +0100 Subject: [PATCH] fmt: add a space after + operator/method overload (#7453) --- doc/docs.md | 4 ++-- vlib/time/operator.v | 2 +- vlib/v/ast/str.v | 3 +++ .../tests/modules/overload_return_type/point.v | 2 +- vlib/v/fmt/tests/operator_overload_keep.v | 11 +++++++++++ 5 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 vlib/v/fmt/tests/operator_overload_keep.v diff --git a/doc/docs.md b/doc/docs.md index 03cc391115..e533017a70 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -3074,11 +3074,11 @@ fn (a Vec) str() string { return '{$a.x, $a.y}' } -fn (a Vec) +(b Vec) Vec { +fn (a Vec) + (b Vec) Vec { return Vec{a.x + b.x, a.y + b.y} } -fn (a Vec) -(b Vec) Vec { +fn (a Vec) - (b Vec) Vec { return Vec{a.x - b.x, a.y - b.y} } diff --git a/vlib/time/operator.v b/vlib/time/operator.v index c944fbdfc1..77142eeafa 100644 --- a/vlib/time/operator.v +++ b/vlib/time/operator.v @@ -47,7 +47,7 @@ pub fn (t1 Time) ge(t2 Time) bool { // Time subtract using eperator overloading [inline] -pub fn (lhs Time) -(rhs Time) Duration { +pub fn (lhs Time) - (rhs Time) Duration { lhs_micro := lhs.unix * 1000 * 1000 + u64(lhs.microsecond) rhs_micro := rhs.unix * 1000 * 1000 + u64(rhs.microsecond) return (i64(lhs_micro) - i64(rhs_micro)) * microsecond diff --git a/vlib/v/ast/str.v b/vlib/v/ast/str.v index 98dde37f56..3671643102 100644 --- a/vlib/v/ast/str.v +++ b/vlib/v/ast/str.v @@ -50,6 +50,9 @@ pub fn (node &FnDecl) stringify(t &table.Table, cur_mod string) string { name = 'JS.$name' } f.write('fn $receiver$name') + if name in ['+', '-', '*', '/', '%'] { + f.write(' ') + } if node.is_generic { f.write('') } diff --git a/vlib/v/checker/tests/modules/overload_return_type/point.v b/vlib/v/checker/tests/modules/overload_return_type/point.v index f0ccbf43ce..a26932bd41 100644 --- a/vlib/v/checker/tests/modules/overload_return_type/point.v +++ b/vlib/v/checker/tests/modules/overload_return_type/point.v @@ -6,6 +6,6 @@ mut: y int } -pub fn (a Point) +(b Point) int { +pub fn (a Point) + (b Point) int { return a.x + b.x } diff --git a/vlib/v/fmt/tests/operator_overload_keep.v b/vlib/v/fmt/tests/operator_overload_keep.v new file mode 100644 index 0000000000..381224a369 --- /dev/null +++ b/vlib/v/fmt/tests/operator_overload_keep.v @@ -0,0 +1,11 @@ +struct Foo { + x int +} + +fn (a Foo) + (b Foo) Foo { + return Foo{a.x + b.x} +} + +fn (a Foo) % (b Foo) Foo { + return Foo{a.x % b.x} +}