Skip to content

Commit

Permalink
all: change optional to result in most of the libraries (#16123)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Oct 20, 2022
1 parent 0d36856 commit 51f4d99
Show file tree
Hide file tree
Showing 75 changed files with 439 additions and 446 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/v_apps_and_modules_compile.yml
Expand Up @@ -81,16 +81,16 @@ jobs:
echo "Test libsodium"
VJOBS=1 v test ~/.vmodules/libsodium
- name: Build VEX
run: |
echo "Install Vex"
v install nedpals.vex
echo "Compile all of the Vex examples"
v should-compile-all ~/.vmodules/nedpals/vex/examples
echo "Compile the simple Vex example with -gc boehm -skip-unused"
v -gc boehm -skip-unused ~/.vmodules/nedpals/vex/examples/simple_example.v
echo "Run Vex Tests"
v test ~/.vmodules/nedpals/vex
## - name: Build VEX
## run: |
## echo "Install Vex"
## v install nedpals.vex
## echo "Compile all of the Vex examples"
## v should-compile-all ~/.vmodules/nedpals/vex/examples
## echo "Compile the simple Vex example with -gc boehm -skip-unused"
## v -gc boehm -skip-unused ~/.vmodules/nedpals/vex/examples/simple_example.v
## echo "Run Vex Tests"
## v test ~/.vmodules/nedpals/vex

- name: Build go2v
run: |
Expand Down
2 changes: 1 addition & 1 deletion cmd/tools/vdoc/tests/vdoc_file_test.v
Expand Up @@ -16,7 +16,7 @@ fn find_diff_cmd() string {

fn test_vet() {
os.setenv('VCOLORS', 'never', true)
os.chdir(vroot)?
os.chdir(vroot)!
test_dir := 'cmd/tools/vdoc/tests/testdata'
main_files := get_main_files_in_dir(test_dir)
fails := check_path(vexe, test_dir, main_files)
Expand Down
2 changes: 1 addition & 1 deletion cmd/tools/vdoc/utils.v
Expand Up @@ -70,7 +70,7 @@ fn set_output_type_from_str(format string) OutputType {
return output_type
}

fn get_ignore_paths(path string) ?[]string {
fn get_ignore_paths(path string) ![]string {
ignore_file_path := os.join_path(path, '.vdocignore')
ignore_content := os.read_file(ignore_file_path) or {
return error_with_code('ignore file not found.', 1)
Expand Down
6 changes: 3 additions & 3 deletions examples/cli.v
Expand Up @@ -41,7 +41,7 @@ fn main() {
cmd.parse(os.args)
}

fn greet_func(cmd Command) ? {
fn greet_func(cmd Command) ! {
language := cmd.flags.get_string('language') or { panic('Failed to get `language` flag: $err') }
times := cmd.flags.get_int('times') or { panic('Failed to get `times` flag: $err') }
name := cmd.args[0]
Expand Down Expand Up @@ -69,10 +69,10 @@ fn greet_func(cmd Command) ? {
}
}

fn greet_pre_func(cmd Command) ? {
fn greet_pre_func(cmd Command) ! {
println('This is a function running before the main function.\n')
}

fn greet_post_func(cmd Command) ? {
fn greet_post_func(cmd Command) ! {
println('\nThis is a function running after the main function.')
}
2 changes: 1 addition & 1 deletion examples/vpwgen.v
Expand Up @@ -10,7 +10,7 @@ fn main() {
for _ in 0 .. repeats {
mut sb := strings.new_builder(blocksize)
for {
x := rand.read(blocksize)?
x := rand.read(blocksize)!
for c in x {
if c >= `0` && c <= `~` {
sb.write_u8(c)
Expand Down
6 changes: 3 additions & 3 deletions vlib/arrays/README.md
Expand Up @@ -9,8 +9,8 @@ import arrays
fn main() {
a := [1, 5, 7, 0, 9]
assert arrays.min(a)? == 0
assert arrays.max(a)? == 9
assert arrays.idx_min(a)? == 3
assert arrays.min(a)! == 0
assert arrays.max(a)! == 9
assert arrays.idx_min(a)! == 3
}
```
12 changes: 6 additions & 6 deletions vlib/arrays/arrays.v
Expand Up @@ -200,7 +200,7 @@ pub fn window<T>(array []T, attr WindowAttribute) [][]T {
// which means you can only pass array of numbers for now.
// TODO: Fix generic operator overloading detection issue.
// Example: arrays.sum<int>([1, 2, 3, 4, 5])? // => 15
pub fn sum<T>(array []T) ?T {
pub fn sum<T>(array []T) !T {
if array.len == 0 {
return error('Cannot sum up array of nothing.')
} else {
Expand All @@ -222,8 +222,8 @@ pub fn sum<T>(array []T) ?T {
// returns the accumulated value in `acc`.
// returns an error if the array is empty.
// See also: [fold](#fold).
// Example: arrays.reduce([1, 2, 3, 4, 5], fn (t1 int, t2 int) int { return t1 * t2 })? // => 120
pub fn reduce<T>(array []T, reduce_op fn (acc T, elem T) T) ?T {
// Example: arrays.reduce([1, 2, 3, 4, 5], fn (t1 int, t2 int) int { return t1 * t2 })! // => 120
pub fn reduce<T>(array []T, reduce_op fn (acc T, elem T) T) !T {
if array.len == 0 {
return error('Cannot reduce array of nothing.')
} else {
Expand All @@ -245,7 +245,7 @@ pub fn reduce<T>(array []T, reduce_op fn (acc T, elem T) T) ?T {
// returns the accumulated value in `acc`.
// returns an error if the array is empty.
// See also: [fold_indexed](#fold_indexed).
pub fn reduce_indexed<T>(array []T, reduce_op fn (idx int, acc T, elem T) T) ?T {
pub fn reduce_indexed<T>(array []T, reduce_op fn (idx int, acc T, elem T) T) !T {
if array.len == 0 {
return error('Cannot reduce array of nothing.')
} else {
Expand Down Expand Up @@ -427,8 +427,8 @@ pub fn lower_bound<T>(array []T, val T) !T {
}

// returns the largest element <= val, requires `array` to be sorted
// Example: arrays.upper_bound([2, 4, 6, 8], 3)? // => 2
pub fn upper_bound<T>(array []T, val T) ?T {
// Example: arrays.upper_bound([2, 4, 6, 8], 3)! // => 2
pub fn upper_bound<T>(array []T, val T) !T {
if array.len == 0 {
return error('.upper_bound called on an empty array')
}
Expand Down
12 changes: 6 additions & 6 deletions vlib/arrays/arrays_test.v
Expand Up @@ -14,7 +14,7 @@ fn test_min() {
c := [u8(4), 9, 3, 1]
mut rb := min(c)!
assert rb == u8(1)
rb = min(c[..3])?
rb = min(c[..3])!
assert rb == u8(3)
}

Expand Down Expand Up @@ -243,26 +243,26 @@ fn test_concat_string() {

fn test_binary_search() {
a := [1, 3, 3, 4, 5, 6, 7, 8, 10]
assert binary_search(a, 3)? == 1
assert binary_search(a, 3)! == 1
assert (binary_search(a, 0) or { -1 }) == -1
}

fn test_lower_bound() {
a := [1, 3, 3, 4, 5, 6, 7, 8, 10]
b := []int{}
c := [1, 2, 3]
assert lower_bound(a, 2)? == 3
assert lower_bound(a, 2)! == 3
assert (lower_bound(b, 4) or { -1 }) == -1
assert lower_bound(c, 3)? == 3
assert lower_bound(c, 3)! == 3
}

fn test_upper_bound() {
a := [1, 3, 3, 4, 5, 6, 7, 8, 10]
b := []int{}
c := [1, 2, 3]
assert upper_bound(a, 9)? == 8
assert upper_bound(a, 9)! == 8
assert (upper_bound(b, 4) or { -1 }) == -1
assert upper_bound(c, 2)? == 2
assert upper_bound(c, 2)! == 2
}

fn test_rotate_right() {
Expand Down
4 changes: 2 additions & 2 deletions vlib/cli/README.md
Expand Up @@ -18,14 +18,14 @@ fn main() {
mut app := cli.Command{
name: 'example-app'
description: 'example-app'
execute: fn (cmd cli.Command) ? {
execute: fn (cmd cli.Command) ! {
println('hello app')
return
}
commands: [
cli.Command{
name: 'sub'
execute: fn (cmd cli.Command) ? {
execute: fn (cmd cli.Command) ! {
println('hello subcommand')
return
}
Expand Down
4 changes: 2 additions & 2 deletions vlib/cli/command.v
Expand Up @@ -2,7 +2,7 @@ module cli

import term

type FnCommandCallback = fn (cmd Command) ?
type FnCommandCallback = fn (cmd Command) !

// str returns the `string` representation of the callback.
pub fn (f FnCommandCallback) str() string {
Expand Down Expand Up @@ -311,7 +311,7 @@ pub fn (cmd Command) execute_man() {
}
}

fn (cmds []Command) get(name string) ?Command {
fn (cmds []Command) get(name string) !Command {
for cmd in cmds {
if cmd.name == name {
return cmd
Expand Down
16 changes: 8 additions & 8 deletions vlib/cli/command_test.v
Expand Up @@ -30,7 +30,7 @@ fn test_if_subcommands_parse_args() {
cmd.parse(['command', 'subcommand', 'arg0', 'arg1'])
}

fn if_subcommands_parse_args_func(cmd cli.Command) ? {
fn if_subcommands_parse_args_func(cmd cli.Command) ! {
assert cmd.name == 'subcommand' && compare_arrays(cmd.args, ['arg0', 'arg1'])
}

Expand All @@ -51,8 +51,8 @@ fn test_if_command_has_default_version_subcommand_if_version_is_set() {
assert has_command(cmd, 'version')
}

fn flag_should_be_set(cmd cli.Command) ? {
flag := cmd.flags.get_string('flag')?
fn flag_should_be_set(cmd cli.Command) ! {
flag := cmd.flags.get_string('flag')!
assert flag == 'value'
}

Expand Down Expand Up @@ -95,10 +95,10 @@ fn test_if_flag_gets_set_with_long_arg() {
cmd.parse(['command', '--flag', 'value'])
}

fn flag_should_have_value_of_42(cmd cli.Command) ? {
flag := cmd.flags.get_string('flag')?
fn flag_should_have_value_of_42(cmd cli.Command) ! {
flag := cmd.flags.get_string('flag')!
assert flag == 'value'
value := cmd.flags.get_int('value')?
value := cmd.flags.get_int('value')!
assert value == 42
}

Expand Down Expand Up @@ -135,7 +135,7 @@ fn test_if_required_flags_get_set() {
cmd.parse(['command', '-flag', 'value', '-value', '42'])
}

fn flag_is_set_in_subcommand(cmd cli.Command) ? {
fn flag_is_set_in_subcommand(cmd cli.Command) ! {
flag := cmd.flags.get_string('flag') or { panic(err) }
assert flag == 'value'
}
Expand Down Expand Up @@ -197,7 +197,7 @@ fn test_command_setup() {
}

// helper functions
fn empty_func(cmd cli.Command) ? {
fn empty_func(cmd cli.Command) ! {
}

fn has_command(cmd cli.Command, name string) bool {
Expand Down

0 comments on commit 51f4d99

Please sign in to comment.