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

builtin: cleanup option names (part 1) (#9293)

This commit is contained in:
spaceface
2021-03-14 01:54:46 +01:00
committed by GitHub
parent 5a6b321656
commit 9ed9e7aff0
7 changed files with 67 additions and 84 deletions

View File

@ -52,49 +52,38 @@ pub fn error_with_code3(message string, code int) IError {
}
}
////////////////////////////////////////
// error returns a default error instance containing the error given in `message`.
// Example: `if ouch { return error('an error occurred') }`
[inline]
pub fn error(message string) IError {
return &Error{
msg: message
}
}
// these are just here temporarily to avoid breaking the compiler; they will be removed soon
pub fn error(a string) Option2 { return {} }
pub fn error_with_code(a string, b int) Option2 { return {} }
// error_with_code returns a default error instance containing the given `message` and error `code`.
// `if ouch { return error_with_code('an error occurred', 1) }`
[inline]
pub fn error_with_code(message string, code int) IError {
return &Error {
msg: message
code: code
}
}
// Option2 is the base of V's new internal optional return system.
struct Option2 {
// Option is the base of V's internal optional return system.
struct Option {
state byte
err Error
err IError = none__
// Data is trailing after err
// and is not included in here but in the
// derived Option2_xxx types
// derived Option_xxx types
}
// `fn foo() ?Foo { return foo }` => `fn foo() ?Foo { return opt_ok(foo); }`
fn opt_ok(data voidptr, mut option Option2, size int) {
fn opt_ok(data voidptr, mut option Option, size int) {
unsafe {
*option = Option2{}
*option = Option{}
// use err to get the end of OptionBase and then memcpy into it
C.memcpy(byteptr(&option.err) + sizeof(Error), data, size)
}
}
// 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
}
C.memcpy(byteptr(&option.err) + sizeof(IError), data, size)
}
}