v icon indicating copy to clipboard operation
v copied to clipboard

Cannot use Option/Result with []thread

Open duarteroso opened this issue 2 years ago • 1 comments

Describe the bug

When creating an array of threads it is impossible to catch an Option or Result from it, unlike its non-array counterpart wit allows placing the bang ! operator in the .wait()! cal.

Expected Behavior

It would be sweet to have the same functionality across single/multiple threads

Current Behavior

   15 | }
error_thread.v:14:17: error: unexpected `!`, the function `wait` does not return an Option or a Result
   12 |     mut handlers := []thread{}
   13 |     handlers << spawn foo_error()
   14 |     handlers.wait()!

Using the bang ! operator at the function call also doesn't work

[duarteroso@fedora vlang]$ v run error_thread.v 
error_thread.v:13:31: error: option handling cannot be done in `spawn` call. Do it when calling `.wait()`
   11 | fn fail() ! {
   12 |     mut handlers := []thread{}
   13 |     handlers << spawn foo_error()!
      |                                  ^
   14 |     handlers.wait()
   15 | }

Reproduction Steps

module main

fn foo_error() ! {
}

fn ok() ! {
	handler := spawn foo_error()
	handler.wait()!
}

fn fail() ! {
    mut handlers := []thread{}
	handlers << spawn foo_error()
	handlers.wait()!
}

fn main() {
	ok() or { panic(err) }
	fail() or { panic(err) }
}

Possible Solution

No response

Additional Information/Context

No response

V version

0.3.3

Environment details (OS name and version, etc.)

macOS

duarteroso avatar Mar 11 '23 22:03 duarteroso

With latest V, V 0.4.12 f108077, I get

error_thread.v:13:20: error: cannot append `thread !` to `[]thread`
   11 | fn fail() ! {
   12 |     mut handlers := []thread{}
   13 |     handlers << spawn foo_error()
      |                       ~~~~~~~~~~~
   14 |     handlers.wait()!
   15 | }
error_thread.v:14:17: error: unexpected `!`, the function `wait` does not return a Result
   12 |     mut handlers := []thread{}
   13 |     handlers << spawn foo_error()
   14 |     handlers.wait()!
      |                    ^
   15 | }
   16 |

which makes perfect sense.

You cannot store a Result in an array, as if it is an error is returned, it must be handled immediately.

JalonSolov avatar Dec 07 '25 03:12 JalonSolov