3 ms·
> - No err != nil checks (replaced by option types) Where can I find code illustrating V Option types?
by SaulOfTheJungle 8y ago
> - No err != nil checks (replaced by option types)
Where can I find code illustrating V Option types?
- amedvednikov 8y agohttps://github.com/vlang-io/V/blob/master/examples/users.v https://github.com/vlang-io/V/blob/master/examples/users.v They are very simple and combine Rust's Option<T> and Result<T>.
- Sholmesy 8y ago// `http.get()` returns an optional string. // V optionals combine the features of Rust's Option<T> and Result<T>. // We must unwrap all optionals with `or`, otherwise V will complain. s := http.get(API_URL) or { // `err` is a reserved variable (not a global) that // contains an error message if there is one eprintln('Failed to fetch "users.json": $err') // `or` blocks must end with `return`, `break`, or `continue` return } This looks really handy. Is there somewhere else (outside of V) where I can read about Option types (in the real world, or in theory).
- int_19h 8y agoWhat's the difference between "break" and "continue" inside an or-block? Or do they apply to the outer enclosing loop if there is one?
- amedvednikov 8y agoYes, just like in Swift.
- int_19h 8y agoThen I don't get it - how do you make the or-block exit normally and yield a value, instead of exiting the outer scope one way or another?