1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

builtin: add an Option2 struct (#8890)

This commit is contained in:
spaceface 2021-02-22 17:44:15 +01:00 committed by GitHub
parent 18aecde9e5
commit 8033203ef6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 70 additions and 17 deletions

View File

@ -8,6 +8,6 @@ fn main(){
some_module.do_work()
}
fn on_error(sender voidptr, e &some_module.Error, x voidptr) {
fn on_error(sender voidptr, e &some_module.MyError, x voidptr) {
println(e.message)
}

View File

@ -7,12 +7,12 @@ const (
)
pub struct Work {
pub:
pub:
hours int
}
pub struct Error {
pub:
pub struct MyError {
pub:
message string
}
@ -21,7 +21,7 @@ pub fn do_work(){
for i in 0..20 {
println("working...")
if i == 15 {
error := &Error{"There was an error."}
error := &MyError{"There was an error."}
eb.publish("error", work, error)
eb.publish("error", work, error)
return

View File

@ -3,16 +3,6 @@
// that can be found in the LICENSE file.
module builtin
/*
struct Option2<T> {
ok bool
is_none bool
error string
ecode int
data T
}
*/
// OptionBase is the the base of V's internal optional return system.
struct OptionBase {
ok bool
is_none bool
@ -81,3 +71,65 @@ pub fn error_with_code(message string, code int) Option {
ecode: code
}
}
struct Option2 {
state byte
err Error
}
// OptionBase is the the base of V's internal optional return system.
struct OptionBase2 {
state byte
err Error
// Data is trailing after err
// and is not included in here but in the
// derived Option2_xxx types
}
// Error holds information about an error instance
struct Error {
msg string
code int
}
// /*
pub fn (o Option2) str() string {
if o.state == 0 {
return 'Option2{ ok }'
}
if o.state == 1 {
return 'Option2{ none }'
}
return 'Option2{ err: "$o.err.msg" }'
}
// opt_none is used internally when returning `none`.
fn opt_none2() Option2 {
return Option2{
state: 1
}
}
// error returns an optional containing the error given in `message`.
// `if ouch { return error('an error occurred') }`
pub fn error2(message string) Option2 {
return Option2{
state: 2
err: {
msg: message
}
}
}
// error_with_code returns an optional containing both error `message` and error `code`.
// `if ouch { return error_with_code('an error occurred',1) }`
pub fn error_with_code2(message string, code int) Option2 {
return Option2{
state: 2
err: {
msg: message
code: code
}
}
}
// */

View File

@ -2355,7 +2355,8 @@ pub fn (mut c Checker) return_stmt(mut return_stmt ast.Return) {
}
return_stmt.types = got_types
// allow `none` & `error (Option)` return types for function that returns optional
if exp_is_optional && got_types[0].idx() in [table.none_type_idx, c.table.type_idxs['Option']] {
if exp_is_optional
&& got_types[0].idx() in [table.none_type_idx, c.table.type_idxs['Option'], c.table.type_idxs['Option2']] {
return
}
if expected_types.len > 0 && expected_types.len != got_types.len {

View File

@ -5286,7 +5286,7 @@ fn (mut g Gen) write_init_function() {
}
const (
builtins = ['string', 'array', 'KeyValue', 'DenseArray', 'map', 'Option']
builtins = ['string', 'array', 'KeyValue', 'DenseArray', 'map', 'Option', 'Error', 'Option2']
)
fn (mut g Gen) write_builtin_types() {