-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
checker: disallow option or result return type in operator overloading
- Loading branch information
Showing
3 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
vlib/v/checker/tests/operator_overloading_return_type_option_or_result.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
vlib/v/checker/tests/operator_overloading_return_type_option_or_result.vv:3:22: error: return type cannot be Option or Result | ||
1 | type Vec = []int | ||
2 | | ||
3 | fn (v Vec) + (u Vec) !Vec { | ||
| ~~~~ | ||
4 | if v.len != u.len { | ||
5 | return error("Operations require dim(v) == dim(u)") | ||
vlib/v/checker/tests/operator_overloading_return_type_option_or_result.vv:10:22: error: return type cannot be Option or Result | ||
8 | } | ||
9 | | ||
10 | fn (v Vec) - (u Vec) ?Vec { | ||
| ~~~~ | ||
11 | if v.len != u.len { | ||
12 | return none |
22 changes: 22 additions & 0 deletions
22
vlib/v/checker/tests/operator_overloading_return_type_option_or_result.vv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
type Vec = []int | ||
|
||
fn (v Vec) + (u Vec) !Vec { | ||
if v.len != u.len { | ||
return error("Operations require dim(v) == dim(u)") | ||
} | ||
return Vec([v[0] + u[0], v[1] + u[1], v[2] + u[2]]) | ||
} | ||
|
||
fn (v Vec) - (u Vec) ?Vec { | ||
if v.len != u.len { | ||
return none | ||
} | ||
return Vec([v[0] + u[0], v[1] + u[1], v[2] + u[2]]) | ||
} | ||
|
||
|
||
|
||
fn main() { | ||
vec := Vec([1, 2, 3]) | ||
dump(vec + vec) | ||
} |