safety.v
Because sometimes optional errors just aren't enough.
Why
The only issue I've had in V is the inability to assign a variable as an optional value. V technically has option types, but they share space with errors and can't be assigned or passed around. This library gives you the ability to use the Option and Result patterns, rather than having to rely soley on returning errors and having them bubble up.
In most cases a library like this probably isn't necessary, but in the cases where it is... well here it is.
Usage
Result
The
Result
Result
fn fetch_results(path string) Result<MyType> {
if res := some_fetch_operation() {
return ok(res)
} else {
return err('Failed to fetch results')
}
}
The main difference between this and V's builtin
error
fetch_results('foo')?
or
Result
Result
if res.is_some() {
// Result contains a value
} else if res.is_none() {
// Result contains an error
}
And you can even go as far as to convert the error into a v
error
res.throw()
Maybe
This was going to be called
Option
Option
Maybe
nil
nil
fn foo() {
// ...
parts := str.split_nth('#', 2)
maybe_id := if parts.len == 2 {
something<string>(parts[1])
} else {
nothing<string>()
}
// ... some other stuff
id := maybe_id.is_something() {
u32(strconv.parse_uint(maybe_id.unwrap(), 16, 32))?
} else {
infer_id(str)
}
// ...
}
Might be too complicated to make a good example, but the basic idea is that when
maybe_id
strconv.parse_uint
0