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

cgen: support for error('abc').str() and printing errors

This commit is contained in:
Delyan Angelov
2020-04-24 18:35:33 +03:00
parent 323ca2b3bb
commit c6a829ce82
3 changed files with 42 additions and 1 deletions

View File

@ -21,6 +21,16 @@ struct Option {
is_none bool
}
pub fn (o Option) str() string {
if o.ok && !o.is_none {
return 'Option{ data: ' + o.data[0..32].hex() + ' }'
}
if o.is_none {
return 'Option{ none }'
}
return 'Option{ error: "${o.error}" }'
}
// `fn foo() ?Foo { return foo }` => `fn foo() ?Foo { return opt_ok(foo); }`
fn opt_ok(data voidptr, size int) Option {
if size >= 400 {
@ -52,4 +62,3 @@ pub fn error_with_code(s string, code int) Option {
ecode: code
}
}